AngularJS to Angular Concepts: Quick Reference
Angular is the name for the Angular of today and tomorrow. AngularJS is the name for all v1.x versions of Angular.
This guide helps you transition from AngularJS to Angular by mapping AngularJS syntax to the equivalent Angular syntax.
See the Angular syntax in this .
Template basics
Templates are the user-facing part of an Angular application and are written in HTML. The following table lists some of the key AngularJS template features with their equivalent Angular template syntax.
AngularJS | Angular |
---|---|
Bindings/interpolationYour favorite hero is: {{vm.favoriteHero}} In AngularJS, an expression in curly braces denotes one-way binding. This binds the value of the element to a property in the controller associated with this template. When using the | Bindings/interpolationYour favorite hero is: {{favoriteHero}} In Angular, a template expression in curly braces still denotes one-way binding. This binds the value of the element to a property of the component. The context of the binding is implied and is always the associated component, so it needs no reference variable. For more information, see the Interpolation section of the Template Syntax page. |
Filters<td>{{movie.title | uppercase}}</td> To filter output in AngularJS templates, use the pipe character (|) and one or more filters. This example filters the | Pipes<td>{{movie.title | uppercase}}</td> In Angular you use similar syntax with the pipe (|) character to filter output, but now you call them pipes. Many (but not all) of the built-in filters from AngularJS are built-in pipes in Angular. For more information, see Filters/pipes below. |
Local variables<tr ng-repeat="movie in vm.movies"> <td>{{movie.title}}</td> </tr> Here, | Input variables<tr *ngFor="let movie of movies"> <td>{{movie.title}}</td> </tr> Angular has true template input variables that are explicitly defined using the For more information, see the ngFor micro-syntax section of the Template Syntax page. |
Template directives
AngularJS provides more than seventy built-in directives for templates. Many of them aren't needed in Angular because of its more capable and expressive binding system. The following are some of the key AngularJS built-in directives and their equivalents in Angular.
AngularJS | Angular |
---|---|
ng-app<body ng-app="movieHunter"> The application startup process is called bootstrapping. Although you can bootstrap an AngularJS app in code, many applications bootstrap declaratively with the | Bootstrappingimport { enableProdMode } from '@angular/core'; import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { AppModule } from './app/app.module'; import { environment } from './environments/environment'; if (environment.production) { enableProdMode(); } platformBrowserDynamic().bootstrapModule(AppModule); import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { AppComponent } from './app.component'; @NgModule({ imports: [ BrowserModule ], declarations: [ AppComponent ], bootstrap: [ AppComponent ] }) export class AppModule { } Angular doesn't have a bootstrap directive. To launch the app in code, explicitly bootstrap the application's root module ( |
ng-class<div ng-class="{active: isActive}"> <div ng-class="{active: isActive, shazam: isImportant}"> In AngularJS, the In the first example, the You can specify multiple classes, as shown in the second example. | ngClass<div [ngClass]="{'active': isActive}"> <div [ngClass]="{'active': isActive, 'shazam': isImportant}"> <div [class.active]="isActive"> In Angular, the In the first example, the You can specify multiple classes, as shown in the second example. Angular also has class binding, which is a good way to add or remove a single class, as shown in the third example. For more information see the Attribute, class, and style bindings section of the Template Syntax page. |
ng-click<button ng-click="vm.toggleImage()"> <button ng-click="vm.toggleImage($event)"> In AngularJS, the In the first example, when the user clicks the button, the The second example demonstrates passing in the | Bind to the
|
ng-controller<div ng-controller="MovieListCtrl as vm"> In AngularJS, the | Component decorator@Component({ selector: 'app-movie-list', templateUrl: './movie-list.component.html', styleUrls: [ './movie-list.component.css' ], }) In Angular, the template no longer specifies its associated controller. Rather, the component specifies its associated template as part of the component class decorator. For more information, see Architecture Overview. |
ng-hide In AngularJS, the | Bind to the |
ng-href<a ng-href="{{ angularDocsUrl }}">Angular Docs</a> The In AngularJS, the <a ng-href="#{{ moviesHash }}">Movies</a> Routing is handled differently in Angular. | Bind to the
|
ng-if<table ng-if="movies.length"> In AngularJS, the In this example, the | *ngIf<table *ngIf="movies.length"> The In this example, the The (*) before |
ng-model<input ng-model="vm.favoriteHero"/> In AngularJS, the | ngModel<input [(ngModel)]="favoriteHero" /> In Angular, two-way binding is denoted by For more information on two-way binding with |
ng-repeat<tr ng-repeat="movie in vm.movies"> In AngularJS, the In this example, the table row ( | *ngFor<tr *ngFor="let movie of movies"> The Notice the other syntax differences: The (*) before For more information, see Structural Directives. |
ng-show<h3 ng-show="vm.favoriteHero"> Your favorite hero is: {{vm.favoriteHero}} </h3> In AngularJS, the In this example, the | Bind to the
|
ng-src<img ng-src="{{movie.imageurl}}"> The | Bind to the
|
ng-style<div ng-style="{color: colorPreference}"> In AngularJS, the In the example, the | ngStyle<div [ngStyle]="{'color': colorPreference}"> <div [style.color]="colorPreference"> In Angular, the In the first example, the Angular also has style binding, which is good way to set a single style. This is shown in the second example. For more information on style binding, see the Style binding section of the Template Syntax page. For more information on the |
ng-switch<div ng-switch="vm.favoriteHero && vm.checkMovieHero(vm.favoriteHero)"> <div ng-switch-when="true"> Excellent choice! </div> <div ng-switch-when="false"> No movie, sorry! </div> <div ng-switch-default> Please enter your favorite hero. </div> </div> In AngularJS, the In this example, if | ngSwitch<span [ngSwitch]="favoriteHero && checkMovieHero(favoriteHero)"> <p *ngSwitchCase="true"> Excellent choice! </p> <p *ngSwitchCase="false"> No movie, sorry! </p> <p *ngSwitchDefault> Please enter your favorite hero. </p> </span> In Angular, the In this example, if The (*) before For more information, see The NgSwitch directives section of the Template Syntax page. |
Filters/pipes
Angular pipes provide formatting and transformation for data in the template, similar to AngularJS filters. Many of the built-in filters in AngularJS have corresponding pipes in Angular. For more information on pipes, see Pipes.
AngularJS | Angular |
---|---|
currency<td>{{movie.price | currency}}</td> Formats a number as currency. | currency<td>{{movie.price | currency:'USD':true}}</td> The Angular |
date<td>{{movie.releaseDate | date}}</td> Formats a date to a string based on the requested format. | date<td>{{movie.releaseDate | date}}</td> The Angular |
filter<tr ng-repeat="movie in movieList | filter: {title:listFilter}"> Selects a subset of items from the defined collection, based on the filter criteria. | noneFor performance reasons, no comparable pipe exists in Angular. Do all your filtering in the component. If you need the same filtering code in several templates, consider building a custom pipe. |
json<pre>{{movie | json}}</pre> Converts a JavaScript object into a JSON string. This is useful for debugging. | json<pre>{{movie | json}}</pre> The Angular |
limitTo<tr ng-repeat="movie in movieList | limitTo:2:0"> Selects up to the first parameter (2) number of items from the collection starting (optionally) at the beginning index (0). | slice<tr *ngFor="let movie of movies | slice:0:2"> The |
lowercase<td>{{movie.title | lowercase}}</td> Converts the string to lowercase. | lowercase<td>{{movie.title | lowercase}}</td> The Angular |
number<td>{{movie.starRating | number}}</td> Formats a number as text. | number<td>{{movie.starRating | number}}</td> <td>{{movie.starRating | number:'1.1-2'}}</td> <td>{{movie.approvalRating | percent: '1.0-2'}}</td> The Angular Angular also has a |
orderBy<tr ng-repeat="movie in movieList | orderBy : 'title'"> Displays the collection in the order specified by the expression. In this example, the movie title orders the | noneFor performance reasons, no comparable pipe exists in Angular. Instead, use component code to order or sort results. If you need the same ordering or sorting code in several templates, consider building a custom pipe. |
Modules/controllers/components
In both AngularJS and Angular, modules help you organize your application into cohesive blocks of functionality.
In AngularJS, you write the code that provides the model and the methods for the view in a controller. In Angular, you build a component.
Because much AngularJS code is in JavaScript, JavaScript code is shown in the AngularJS column. The Angular code is shown using TypeScript.
AngularJS | Angular |
---|---|
IIFE(function () { ... }()); In AngularJS, an immediately invoked function expression (or IIFE) around controller code keeps it out of the global namespace. | noneThis is a nonissue in Angular because ES 2015 modules handle the namespacing for you. For more information on modules, see the Modules section of the Architecture Overview. |
Angular modulesangular.module("movieHunter", ["ngRoute"]); In AngularJS, an Angular module keeps track of controllers, services, and other code. The second argument defines the list of other modules that this module depends upon. | NgModulesimport { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { AppComponent } from './app.component'; @NgModule({ imports: [ BrowserModule ], declarations: [ AppComponent ], bootstrap: [ AppComponent ] }) export class AppModule { } NgModules, defined with the For more information on modules, see NgModules. |
Controller registrationangular .module("movieHunter") .controller("MovieListCtrl", ["movieService", MovieListCtrl]); AngularJS has code in each controller that looks up an appropriate Angular module and registers the controller with that module. The first argument is the controller name. The second argument defines the string names of all dependencies injected into this controller, and a reference to the controller function. | Component decorator@Component({ selector: 'app-movie-list', templateUrl: './movie-list.component.html', styleUrls: [ './movie-list.component.css' ], }) Angular adds a decorator to the component class to provide any required metadata. The This is how you associate a template with logic, which is defined in the component class. For more information, see the Components section of the Architecture Overview page. |
Controller functionfunction MovieListCtrl(movieService) { } In AngularJS, you write the code for the model and methods in a controller function. | Component classexport class MovieListComponent { } In Angular, you create a component class to contain the data model and control methods. Use the TypeScript For more information, see the Components section of the Architecture Overview page. |
Dependency injectionMovieListCtrl.$inject = ['MovieService']; function MovieListCtrl(movieService) { } In AngularJS, you pass in any dependencies as controller function arguments. This example injects a To guard against minification problems, tell Angular explicitly that it should inject an instance of the | Dependency injectionconstructor(movieService: MovieService) { } In Angular, you pass in dependencies as arguments to the component class constructor. This example injects a For more information, see the Dependency injection section of the Architecture Overview. |
Style sheets
Style sheets give your application a nice look. In AngularJS, you specify the style sheets for your entire application. As the application grows over time, the styles for the many parts of the application merge, which can cause unexpected results. In Angular, you can still define style sheets for your entire application. But now you can also encapsulate a style sheet within a specific component.
AngularJS | Angular |
---|---|
Link tag<link href="styles.css" rel="stylesheet" /> AngularJS, uses a | Styles configuration"styles": [ "styles.css" ], With the Angular CLI, you can configure your global styles in the StyleUrls In Angular, you can use the styleUrls: [ './movie-list.component.css' ], This allows you to set appropriate styles for individual components that won’t leak into other parts of the application. |
© 2010–2020 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://v8.angular.io/guide/ajs-quick-reference