Angular and Bootstrap 4 with Sass

Create angular project with Sass support

Angular has support for Sass build in. To create angular project with Sass support use the command:

If you update existing project use the command:

Install Bootstrap 4 with prerequisites

To install bootstrap use command:

Bootstrap 4 require jquery & popper.js lib to be installed:

To check if everything is in place to start using bootstrap check npm modules :

The command “npm list” should not print out any errors.

Using bootstrap and customize it

We need project’s scss main file and import bootstrap in it. Under assets folder create new subfolder “scss” and place new “main.scss” file in it.

Add main.scss file into styles array in the .angular-cli.json file.

Add import statement for bootstrap and the file with customized variables to main.scss.

You can also copy content of the node_modules/bootstrap/scss/_variables.scss file to your project custom _variables.scss file and change it as you wish. Some variables use computational functions for colors etc., so you will need to import file with functions first.

Underline as first character of scss files – partials

If the name of the scss file begin with the underline character, it means that file will not be compiled to CSS file. The file represent partial scss file and it must be imported in primary scss file.  So the variables file is actually named “_custom.scss” and is placed in the assets/scss folder. It is imported into main.scss.

Font-Awesome Sass integration

Bootstrap 4 does not bring any icon font any more so you can use font awesome for example.

Add import statement into main.scss file :

Sample usage:

Separate style from html

To simplify html I prefer to write style information in separate files.

When we use large CSS libraries as Bootstrap, chances are very high, that we will need to replace them later with another css library or our own developed style theme.

So if you wish to be able to change Bootstrap in the future for something more advanced or modern and still use it today, you create your own CSS class definitions and extend bootstrap classes.

 

For each angular component create separated partial scss file and import it in the main.scss file.

 

Name root html tag of each component with an ID so you can create styles in the hierarchy, valid only for the specific component, without leaking to any other part of the application.

Styles are created in SCSS files inside component id selector:

To use specific bootstrap class as your own, you need to extend it. You can use one or more classes in the single @extend statement.

Create your css selector names with semantic naming, that way your html code will become self descriptive and maintainable in the future.

Component declaration doesn’t use the scss files and we retain default view encapsulation of the component.

The compiled CSS file is only one for whole application.

Redesigned currency example now looks like this :

 

 

 

 

 

 

Angular and Sass with Bootstrap 3 style support

Integrating Sass and Bootstrap 3 to angular project

If you want to customize default bootstrap 3 CSS theme, you will need to change it. But to be able to do that you will need to install CSS extension language Sass and bootstrap-sass definitions and then compile custom bootstrap CSS within your angular application.  This is officially supported by angular-cli tools.

Setup angular project for Sass

If you create new angular project you can add option “–styles=scss” to “new” command. Yo can also change existing project style settings with “set” command:

To compile scss files to css files we need node-sass compiler :

Next we install bootstrap-sass in our project :

Writing CSS with Sass

First we create “scss” subfolder under the “assets” folder and create main.scss file in it.

Angular project expect your main.scss file defined in the .angular-cli.json file:

Start the project and angular-cli will compile SCSS and include result CSS into your application.

Test Sass compiler and angular integration

Add some simple scss definition to main.scss:

Now add a paragraph with “mytitle” class to your index.html :

If everything works  as expected you will see a test paragraph with correct font and weight as defined by scss:

Scss compiler create proper CSS definition and angular with webpack create styles.bundle.js file where we can find our “mytitle” class defined as :

Import Bootstrap Sass definitions

I am changing my existing seed project so you can get source code of this project from github repository “ANGULARMYAPP” .

Immediately after changing style definition to SCSS in our project, we lost all bootstrap CSS formating and the page looks like :

As we see from the screen above, there is no bootstrap formatting anymore, buttons are missing images (font_awesome as part of bootstrap also missing).

Now we will add bootstrap-scss and compile our own version of bootstrap css.

First we create new custom variables file and copy  all bootstrap theme variables to it. That way we will be able to customize any bootstrap setting simply by changing our copy of variable.

Create new file “_custom_variables.scss” and copy content of the bootstrap _variables.scss file into it or just copy a file into your scss folder. All files named with underscore are meant to be imported in another scss file by the Sass standard. Files without underscore are compiled to CSS format.

Now we import bootstrap and you variables file into main.scss file:

If everything works as expected we will get our original bootstrap design back and we can start modifying it, for example we will change primary button color to red. Change variable  $brand-primary in the file _custom_variables.scss to red color:

And the result should be visible immediately on the screen :

The button defined in the currency-list component with the class “btn-primary”  is now red as expected.

 

 

 

 

 

 

Angular – create currency edit component

You can get source code of this project from github repository “ANGULARMYAPP” .

Edit form

What I wish to accomplish is to enable edit function on the currency list component.  Button “Add” will open editing panel, after saving changes the  editing panel will close.

The Pencil open source product is used for GUI prototyping.

To create new component we use angular command line interface and generate new component skeleton.

After that we add new component to the currency-list.component template.

Some design considerations

New component will contain edit form with two buttons (save and cancel). After adding or editing record the form will be closed automatically. After some changes will be made, the list component will refresh it’s data source and the table will refresh it on the screen.

After we look at actions needed in the workflow, it’s obvious that the best way is to develop component with two events: save and cancel. The component will work only with events, that way no service interaction will be needed. All interaction will remain in the list component where we already have back-end service injected at the constructor.

Edit component template file

Edit component class file

The component is really simple one. I am surprised how little is needed to create a pretty component with binding and events in the angular framework !

The component is included in the list component with component tag just above the table component and under the “Add” button :

In the list component we intercept two events (cancel and save) and call to service layer for back-end actions. With the currency variable we push the selected row to the edit component (this is one way binding).

After each change in the database we refresh the table with changed data.

List component class

And the final template for currency-list component

Because every client/server interaction is now asynchronous, the code in the list view component become less readable and little harder to understand. If you need to add any code after the call to the server, you need to add it to the subscribe success part of the observable execution path.

Don’t forget, you can always set a callback function as a subscribed success call, that way you retain readability even in more complex scenarios.

I just open the curly brackets after the fat arrow function and add multiple statements for this simple example.

The final result

If you click on the “Add” button, the edit form will open and you will be able to add new record to the list.

Edit form will show only input fields enabled for edit (no “Id” field for example).

If you click on the pencil button on a specific row, you open the edit form with the selected data. You can then save or cancel changes. After the action, the edit form is closed (removed from the DOM because of *ngIf directive), and the data are refreshed automatically.

Of course this is only a “TODO” like application and intentionally I didn’t put router into it to remain as simple as possible.

There is always more what can be done, for example edit and delete icons are always enabled, even after the edit form is open. At the edit form, there are no real validation rules before save button can be clicked, there is no error message event and messages don’t flow up in the case of validation errors in the edit component etc. However, that are all interesting challenges for later.

Angular – create currency list view component

You can get source code of this project from github repository “ANGULARMYAPP” .

Build components

In previous blog article I describe angular environment and generate base angular skeleton application. In this article I will create my first list component and connect client app to server REST service.

Create new component

When you create new component, you will need to name it first. You can use dash in the middle of the name, but it is not required. The dash will not be used by generator, the name will be converted by pascal convention (camel case). But your file names and folder names will remain readability.

Use angular cli interface and create skeleton for a component:

Result of the command in the terminal window will look something like this:

The app.module file is updated automatically and new component is registered to angular application:

The result in the project folder hierarchy shown new sub-folder “currency-list” with all corresponding  files:

Open app.component.html and replace generated content with new component tag like:

Now start serving angular application locally :

And navigate to http://localhost:4200 :

Yeah, that was easy.

Proxy to java embedded server

To be able to call to the java spring boot server application we need to add proxy configuration as described here. After proxy configuration we should  add “start” command  (“start”: “ng serve –proxy-config proxy-conf.json”) to “package.json” and use “npm start” instead of “ng serve”.

Create currency interface

Create file currency.ts and add currency definition as is returned from server :

Add “HttpModule” to application

We will request data from REST server with this module, therefore we need to add it to the app.module.ts (as imports) :

Create currency service

Well, without list of currencies from back end service there will not be a currency list component. To provide the list of currencies to the local component I need to create new angular client side service class and connect it to back end server.

If you wish generate to specific folder, you define folder as a relative path to app folder in the name of new service :

If you are in the root or the source folder of your angular application, you are perceived to be in the app folder.

The currency.service.ts and currency.service.spec.ts files are created in app/currency-list folder.

The service will provide a Observable of the list of currencies from the REST server application written in Java. You will find the code for the server app on this github repository.

Provide new service

Don’t forget to provide new service otherwise you will not be able to use it. You need to import it and add it to the providers array.  If you add new service to the application itself you add into app.module.ts  file, but  you can also add it to the parent component, for example “app.component.ts”.  This way all components in the hierarchy from “app” component down will be able to use the service.

Using the service

New service return “Observable” object, it means we will subscribe to it instead of call it. The method will not be triggered before data are really requested (by binding for example).  Unsubscribing should be automatic when using with angular components, otherwise we need to unsubscribe manually.

And binding to the array inside HTML template:

In the template we use for each directive “*ngFor” and loop over array of ICurrency objects received from java back-end service.

The result of this very simple angular list component is:

Angular – Add Bootstrap to your application

Bootstrap components / styles installation

In the root of your angular project open package.json file and add new dependency for Bootstrap and font-awesome.

Update installed dependencies with npm :

Add new directive for styles in .angular-cli.json configuration

After you build your angular application, new webpack prepared application will contain bootstrap components and font.

Start using bootstrap

Simply use bootstrap class directives in your templates, partial template code:

And the result of the example :