+61 450 303 656 info@newplanetit.com
New Feature in Angular 6

New Feature in Angular 6

The 6.0.0 release of Angular is here! This is a major release focused less on the underlying framework, and more on the toolchain and on making it easier to move quickly with Angular in the future.

As a part of this release, we are synchronizing the major versions going forward for the framework packages (@angular/core@angular/common@angular/compiler, etc), the Angular CLI, and Angular Material + CDK. All are releasing as 6.0.0 today. We made this change to clarify cross compatibility. The minor and patch releases for these projects will be issued based on the project’s needs.

See the full list of changes in our changelogs: frameworkmaterial+cdkcli.

ng update

ng update <package> is a new CLI command that analyzes your package.json and uses its knowledge of Angular to recommend updates to your application. To see it in action, check out our update guide.

Not only will ng update help you adopt the right version of dependencies, and keep your dependencies in sync, but 3rd parties can provide update scripts using schematics. If one of your dependencies provides an ng updateschematic, they can automatically update your code when they need to make breaking changes!

ng update will not replace your package manager, but uses npm or yarn under the hood to manage dependencies. In addition to updating dependencies and peer dependencies, ng update will apply needed transforms to your project.

For example, the command ng update @angular/core will update all of the Angular framework packages as well as RxJS and TypeScript, and will run any schematics available on these packages to keep you up to date. As part of this one command, we’ll automatically install rxjs-compat into your application to make the adoption of RxJS v6 smoother.

We expect to see many more libraries and packages add ng updateschematics over the coming months, and have already heard from enterprise component library teams that are planning to use ng update to push through important changes in an automated way to save their developers time.

Learn more about how the ng update command works. To get started creating your own ng update schematic, take a look at the entry in the package.json of rxjs and its associated collection.json.

ng add

Another new CLI command ng add <package> makes adding new capabilities to your project easy. ng add will use your package manager to download new dependencies and invoke an installation script (implemented as a schematic) which can update your project with configuration changes, add additional dependencies (e.g. polyfills), or scaffold package-specific initialization code.

Try out some of the following on your fresh ng new application:

  • ng add @angular/pwa — Turn your application into a PWA by adding an app manifest and service worker
  • ng add @ng-bootstrap/schematics — Add ng-bootstrap to your application
  • ng add @angular/material — Install and setup Angular Material and theming and register new starter components into ng generate
  • ng add @clr/angular@next— Install and setup Clarity from VMWare
  • ng add @angular/elements — Add the needed document-register-element.js polyfill and dependencies for Angular Elements (see below)

Because ng add is built on top of schematics and the npm registry, our hope is that libraries and the community will help us build a rich ecosystem of ng add supporting packages.

Take a look at Angular Material’s ng-add schematic for an example to help you get started building your own ng-add schematics.

Angular Elements

The first release of Angular Elements is focused on allowing you to bootstrap Angular components within an existing Angular application by registering them as Custom Elements. We use this extensively in angular.io as part of our content management system to allow dynamic bootstrapping of capabilities via embedded HTML. This replaces the need to manually bootstrap Angular components found in static html content.

Check out an example of registering a component as a custom element or learn more about Angular Elements.

One of our community members has also produced an Angular Elements Quick Start video that we highly recommend.

Angular Material + CDK Components

The biggest addition is the new tree component for displaying hierarchical data. Following patterns from the data-table component, the CDK houses the core tree directives, with Angular Material offering the same experience with Material Design styles on top. We recently gave a talk about the component, so check that out for more information (videoslides). These new tree components come in both styled (Material’s mat-tree) and unstyled versions (CDK’s cdk-tree).

Alongside the tree, we also have new badge and bottom-sheet components. Badges help display small bits of helpful information, such as unread item counts. Bottom-sheets are a special type of mobile-centric dialogs that come up from the bottom of the viewport, commonly used to present a list of options following an action.

The @angular/cdk/overlay package is one of the most powerful pieces of infrastructure in the CDK today. With the release of v6, this package now includes new positioning logic that helps make pop-ups that intelligently remain on-screen in all situations.

Angular Material Starter Components

Once you have run ng add @angular/material to add material to an existing application, you will also be able to generate 3 new starter components.

Material Sidenav

You can now generate a starter component including a toolbar with the app name and the side navigation. This component is responsive based on breakpoints.


ng generate @angular/material:material-nav --name=my-nav

This will create this starter component:

Material Dashboard

You can now generate a starter dashboard component containing a dynamic grid list of cards.


ng generate @angular/material:material-dashboard --name=my-dashboard

This will create this starter component:

Material Data Table

You can generate a starter data table component that is pre-configured with a datasource for sorting and pagination.


ng generate @angular/material:material-table --name=my-table

This will create this starter component:

CLI Workspaces

CLI v6 now has support for workspaces containing multiple projects, such as multiple applications or libraries. CLI projects will now use angular.jsoninstead of .angular-cli.json for build and project configuration.

Each CLI workspace has projects, each project has targets, and each target can have configurations.

Library Support

One of the most requested features for our CLI has been support for creating and building libraries, and we are proud to introduce:

ng generate library <name>

This command will create a library project within your CLI workspace, and configure it for testing and for building.

Learn more about creating libraries with the Angular CLI

Tree Shakable Providers

To make your applications smaller, we’ve moved from modules referencing services to services referencing modules. This allows us to only bundle services into your code base in modules where they are injected.



No references are needed in our NgModule.

Animations Performance Improvements

We’ve updated our implementation of Animations to no longer need the web animations polyfill. This means that you can remove this polyfill from your application and save approximately 47KB of bundle size, while increasing animations performance in Safari at the same time.

RxJS v6

Angular has been updated to use v6 of RxJS. RxJS is an independent project that released v6 several weeks ago. RxJS v6 brings with it several major changes, along with a backwards compatibility package rxjs-compat that will keep your applications working.

RxJS has been rearranged to make it more tree-shakable, ensuring that only the pieces of RxJS that you use are included in your production bundles.

If you use ng update, your application should keep working, but you can learn more about the 5.5 to 6.0 migration.

Long Term Support (LTS)

We are expanding our Long Term Support to all major releases.

Previously we announced that only v4 and v6 would be LTS releases but in order to make updating from one major to the next easier, and give bigger projects more time to plan updates, we have decided to extend the long-term support to all major releases starting with v4.

Each major release will be supported for 18 months with around 6 months of active development followed by 12 months of critical bugfixes and security patches.

Learn more about how Angular versions and releases.

How to update to 6.0.0

Visit update.angular.io for information and guidance on updating your application.

The update generally follows 3 steps, and will take advantage of the new ng update tool.

  1. Update @angular/cli
  2. Update your Angular framework packages
  3. Update other dependencies

Making it easy for developers to stay up to date with the latest releases is extremely important to us, so let us know what you think about this release in the comments!

What about Ivy?

At ng-conf we mentioned a new initiative called Ivy — our next generation rendering pipeline. Ivy is currently under active development and is not part of the 6.0 release. We will announce an opt-in preview of Ivy as soon as it is ready in the coming months. Keep an eye on this blog for the latest information.

Sharing Data Between Angular Components – Four Methods

Sharing Data Between Angular Components – Four Methods

Health Check: This lesson was last reviewed on  and tested with these packages:

  • Angular v6.0.3
  • RxJS v6.2

Update Notes: Changed imports for RxJS v6.2

Data sharing is an essential concept to understand before diving into your first Angular project. In this lesson, I provide four different methods for sharing data between Angular components.

The Parent-Child-Sibling structure of our Angular app.

Parent to Child: Sharing Data via Input


This is probably the most common and straightforward method of sharing data. It works by using the @Input() decorator to allow data to be passed via the template.

# parent.component.ts

# child.component.ts

Child to Parent: Sharing Data via ViewChild

ViewChild allows one component to be injected into another, giving the parent access to its attributes and functions. One caveat, however, is that child won’t be available until after the view has been initialized. This means we need to implement the AfterViewInit lifecycle hook to receive the data from the child.

# parent.component.ts

# child.component.ts

Child to Parent: Sharing Data via Output() and EventEmitter

Another way to share data is to emit data from the child, which can be listed to by the parent. This approach is ideal when you want to share data changes that occur on things like button clicks, form entires, and other user events.

In the parent, we create a function to receive the message and set it equal to the message variable.

In the child, we declare a messageEvent variable with the Output decorator and set it equal to a new event emitter. Then we create a function named sendMessage that calls emit on this event with the message we want to send. Lastly, we create a button to trigger this function.

The parent can now subscribe to this messageEvent that’s outputted by the child component, then run the receive message function whenever this event occurs.

# parent.component.ts

# child.component.ts

Unrelated Components: Sharing Data with a Service

When passing data between components that lack a direct connection, such as siblings, grandchildren, etc, you should you a shared service. When you have data that should always be in sync, I find the RxJS BehaviorSubject very useful in this situation.

You can also use a regular RxJS Subject for sharing data via the service, but here’s why I prefer a BehaviorSubject.

  • It will always return the current value on subscription – there is no need to call onnext
  • It has a getValue() function to extract the last value as raw data.
  • It ensures that the component always receives the most recent data.

In the service, we create a private BehaviorSubject that will hold the current value of the message. We define a currentMessage variable handle this data stream as an observable that will be used by the components. Lastly, we create a function that calls next on the BehaviorSubject to change its value.

The parent, child, and sibling components all receive the same treatment. We inject the DataService in the constructor, then subscribe to the currentMessage observable and set its value equal to the message variable.

Now if we create a function in any one of these components that changes the value of the message. when this function is executed the new data it’s automatically broadcast to all other components.

# data.service.ts

# parent.component.ts

# sibling.component.ts

Guards and Login Redirects in Angular

Guards and Login Redirects in Angular

Redirect the user to the page they landed on before being forced to login

In your web application, you likely require a user to login to access some functionality. With Angular, we can implement this flow using route guards and the router to help manage redirects.

Basic Route Guard

Imagine we’re building a forum, and we want to ensure that a user is logged in before they can post a new message. You could have the following routes in your application.

The route to the post is the one we want to protect, as we want to allow people to read forums without being logged in. To protect it, we’ll need to create a route guard, which allows us to run a check (or any number of checks) before a route is activated.

There are several types of guards, but the one we’ll use is the canActivate type, which is run before you navigate to a route. These are created like any other service, but they must implement the canActivate method. Here is one such example.

Once we attach this guard to a route, the canActivate() method will fire before the route is activated. The logic will first check if the user is valid (using whatever logic you might need), and if not it will navigate to the login route. Note it also grabs the current URL and sets is as a query parameter, so it would be something like /login?return=%2Fforums%2F2-introductions (which the route becomes URL encoded). Now, we can attach this guard to our route.

A route can have multiple guards, so you must assign a new property on the route with an array of guards. Each type of guard has its own property that you’ll use.

The last bit is to redirect the user after login, which is handled in the login component. This component has a login() method that handles authentication logic, and once authenticated it will redirect you.

Here we use navigateByUrl() to handle the redirect navigation. This takes a raw URL and routes to it, instead of using the typical naviate() method that takes an array of paths.

There are certainly a number of ways to customize this flow for your own purposes. For example, you could store the return URL in a cookie or localstorage instead. I personally like using the URL because I believe that is where navigation state should be captured.