2019-12-02 15:47:19 -05:00
# AngularJS to Angular concepts: Quick reference
2017-02-22 13:09:39 -05:00
{@a top}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
_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.
2017-03-31 19:57:13 -04:00
2017-04-21 20:21:45 -04:00
**See the Angular syntax in this < live-example name = "ajs-quick-reference" > < / live-example > **.
2017-02-22 13:09:39 -05:00
## 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.
2017-03-30 15:04:18 -04:00
2017-02-22 13:09:39 -05:00
< table width = "100%" >
< col width = "50%" >
< / col >
< col width = "50%" >
< / col >
< tr >
< th >
AngularJS
< / th >
< th >
Angular
< / th >
< / tr >
< tr style = top >
< td >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
### Bindings/interpolation
2017-10-23 09:47:21 -04:00
2017-05-09 16:01:04 -04:00
< code-example hideCopy >
2017-02-22 13:09:39 -05:00
Your favorite hero is: {{vm.favoriteHero}}
< / code-example >
2017-03-31 19:57:13 -04:00
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 `controller as` syntax,
the binding is prefixed with the controller alias (`vm` or `$ctrl` ) because you
have to be specific about the source of the binding.
2017-02-22 13:09:39 -05:00
< / td >
< td >
2017-03-31 19:57:13 -04:00
2017-03-31 07:23:16 -04:00
### Bindings/interpolation
2017-03-27 11:08:53 -04:00
2019-07-20 13:40:17 -04:00
< code-example hideCopy path = "ajs-quick-reference/src/app/movie-list.component.html" region = "interpolation" > < / code-example >
2017-03-31 19:57:13 -04:00
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.
2020-04-28 16:26:58 -04:00
For more information, see the [Interpolation ](guide/interpolation ) guide.
2017-02-22 13:09:39 -05:00
< / td >
< / tr >
< tr style = top >
< td >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
### Filters
2017-10-23 09:47:21 -04:00
2017-05-09 16:01:04 -04:00
< code-example hideCopy >
2017-02-22 13:09:39 -05:00
< td>{{movie.title | uppercase}}< /td>
< / code-example >
2017-03-31 19:57:13 -04:00
To filter output in AngularJS templates, use the pipe character (|) and one or more filters.
This example filters the `title` property to uppercase.
2017-02-22 13:09:39 -05:00
< / td >
< td >
2017-03-31 19:57:13 -04:00
2017-03-31 07:23:16 -04:00
### Pipes
2017-03-27 11:08:53 -04:00
2019-07-20 13:40:17 -04:00
< code-example hideCopy path = "ajs-quick-reference/src/app/app.component.html" region = "uppercase" > < / code-example >
2017-03-31 19:57:13 -04:00
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 ](guide/ajs-quick-reference#filters-pipes ) below.
2017-02-22 13:09:39 -05:00
< / td >
< / tr >
< tr style = top >
< td >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
### Local variables
2017-10-23 09:47:21 -04:00
2017-05-09 16:01:04 -04:00
< code-example hideCopy format = "" >
2017-03-31 19:57:13 -04:00
< tr ng-repeat="movie in vm.movies">
< td>{{movie.title}}< /td>
< /tr>
2017-02-22 13:09:39 -05:00
< / code-example >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
Here, `movie` is a user-defined local variable.
< / td >
< td >
2017-03-31 19:57:13 -04:00
2017-03-31 07:23:16 -04:00
### Input variables
2017-03-27 11:08:53 -04:00
2019-07-20 13:40:17 -04:00
< code-example hideCopy path = "ajs-quick-reference/src/app/app.component.html" region = "local" > < / code-example >
2017-03-31 19:57:13 -04:00
Angular has true template input variables that are explicitly defined using the `let` keyword.
2020-04-28 16:26:58 -04:00
For more information, see the [ngFor micro-syntax ](guide/built-in-directives#microsyntax )
section of the [Built-in Directives ](guide/built-in-directives ) page.
2017-02-22 13:09:39 -05:00
< / td >
< / tr >
< / table >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
## 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.
2017-03-30 15:04:18 -04:00
2017-02-22 13:09:39 -05:00
< table width = "100%" >
< col width = "50%" >
< / col >
< col width = "50%" >
< / col >
< tr >
< th >
AngularJS
< / th >
< th >
Angular
< / th >
< / tr >
< tr style = top >
< td >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
### ng-app
2017-10-23 09:47:21 -04:00
2017-05-09 16:01:04 -04:00
< code-example hideCopy >
2017-02-22 13:09:39 -05:00
< body ng-app="movieHunter">
< / code-example >
2017-03-31 19:57:13 -04:00
The application startup process is called **bootstrapping** .
Although you can bootstrap an AngularJS app in code,
many applications bootstrap declaratively with the `ng-app` directive,
giving it the name of the application's module (`movieHunter`).
2017-02-22 13:09:39 -05:00
< / td >
< td >
2017-03-31 19:57:13 -04:00
2017-03-31 07:23:16 -04:00
### Bootstrapping
2017-03-27 11:08:53 -04:00
2019-07-20 13:40:17 -04:00
< code-example hideCopy path = "ajs-quick-reference/src/main.ts" header = "main.ts" > < / code-example >
2017-03-31 07:23:16 -04:00
< br >
2017-03-27 11:08:53 -04:00
2019-07-20 13:40:17 -04:00
< code-example hideCopy path = "ajs-quick-reference/src/app/app.module.1.ts" header = "app.module.ts" > < / code-example >
2017-03-31 19:57:13 -04:00
Angular doesn't have a bootstrap directive.
To launch the app in code, explicitly bootstrap the application's root module (`AppModule`)
in `main.ts`
and the application's root component (`AppComponent`) in `app.module.ts` .
2017-02-22 13:09:39 -05:00
< / td >
< / tr >
< tr style = top >
< td >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
### ng-class
2017-10-23 09:47:21 -04:00
2017-05-09 16:01:04 -04:00
< code-example hideCopy format = "" >
2017-03-31 19:57:13 -04:00
< div ng-class="{active: isActive}">
< div ng-class="{active: isActive,
shazam: isImportant}">
2017-02-22 13:09:39 -05:00
< / code-example >
2017-03-31 19:57:13 -04:00
In AngularJS, the `ng-class` directive includes/excludes CSS classes
based on an expression. That expression is often a key-value control object with each
key of the object defined as a CSS class name, and each value defined as a template expression
that evaluates to a Boolean value.
In the first example, the `active` class is applied to the element if `isActive` is true.
You can specify multiple classes, as shown in the second example.
2017-02-22 13:09:39 -05:00
< / td >
< td >
2017-03-31 19:57:13 -04:00
2017-03-31 07:23:16 -04:00
### ngClass
2017-03-27 11:08:53 -04:00
2019-07-20 13:40:17 -04:00
< code-example hideCopy path = "ajs-quick-reference/src/app/app.component.html" region = "ngClass" > < / code-example >
2017-03-31 19:57:13 -04:00
In Angular, the `ngClass` directive works similarly.
It includes/excludes CSS classes based on an expression.
In the first example, the `active` class is applied to the element if `isActive` is true.
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.
2020-04-28 16:26:58 -04:00
For more information see [Attribute, class, and style bindings ](guide/attribute-binding ) page.
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
< / td >
< / tr >
< tr style = top >
< td >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
### ng-click
2017-10-23 09:47:21 -04:00
2017-05-09 16:01:04 -04:00
< code-example hideCopy format = "" >
2017-03-31 19:57:13 -04:00
< button ng-click="vm.toggleImage()">
< button ng-click="vm.toggleImage($event)">
2017-02-22 13:09:39 -05:00
< / code-example >
2017-03-31 19:57:13 -04:00
In AngularJS, the `ng-click` directive allows you to specify custom behavior when an element is clicked.
In the first example, when the user clicks the button, the `toggleImage()` method in the controller referenced by the `vm` `controller as` alias is executed.
The second example demonstrates passing in the `$event` object, which provides details about the event
to the controller.
2017-02-22 13:09:39 -05:00
< / td >
< td >
2017-03-31 19:57:13 -04:00
2017-03-31 07:23:16 -04:00
### Bind to the `click` event
2017-03-27 11:08:53 -04:00
2019-07-20 13:40:17 -04:00
< code-example hideCopy path = "ajs-quick-reference/src/app/app.component.html" region = "event-binding" > < / code-example >
2017-03-31 19:57:13 -04:00
AngularJS event-based directives do not exist in Angular.
Rather, define one-way binding from the template view to the component using **event binding** .
For event binding, define the name of the target event within parenthesis and
specify a template statement, in quotes, to the right of the equals. Angular then
sets up an event handler for the target event. When the event is raised, the handler
executes the template statement.
In the first example, when a user clicks the button, the `toggleImage()` method in the associated component is executed.
The second example demonstrates passing in the `$event` object, which provides details about the event
to the component.
For a list of DOM events, see: https://developer.mozilla.org/en-US/docs/Web/Events.
2020-04-28 16:26:58 -04:00
For more information, see the [Event binding ](guide/event-binding ) page.
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
< / td >
< / tr >
< tr style = top >
< td >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
### ng-controller
2017-10-23 09:47:21 -04:00
2017-05-09 16:01:04 -04:00
< code-example hideCopy format = "" >
2017-02-22 13:09:39 -05:00
< div ng-controller="MovieListCtrl as vm">
< / code-example >
2017-03-31 19:57:13 -04:00
In AngularJS, the `ng-controller` directive attaches a controller to the view.
Using the `ng-controller` (or defining the controller as part of the routing) ties the
view to the controller code associated with that view.
2017-02-22 13:09:39 -05:00
< / td >
< td >
2017-03-31 19:57:13 -04:00
2017-03-31 07:23:16 -04:00
### Component decorator
2017-03-27 11:08:53 -04:00
2019-07-20 13:40:17 -04:00
< code-example hideCopy path = "ajs-quick-reference/src/app/movie-list.component.ts" region = "component" > < / code-example >
2017-03-31 19:57:13 -04:00
In Angular, the template no longer specifies its associated controller.
Rather, the component specifies its associated template as part of the component class decorator.
2017-04-30 16:10:32 -04:00
For more information, see [Architecture Overview ](guide/architecture#components ).
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
< / td >
< / tr >
< tr style = top >
< td >
2017-03-31 19:57:13 -04:00
### ng-hide
In AngularJS, the `ng-hide` directive shows or hides the associated HTML element based on
an expression. For more information, see [ng-show ](guide/ajs-quick-reference#ng-show ).
2017-02-22 13:09:39 -05:00
< / td >
< td >
2017-03-31 19:57:13 -04:00
### Bind to the `hidden` property
In Angular, you use property binding; there is no built-in *hide* directive.
For more information, see [ng-show ](guide/ajs-quick-reference#ng-show ).
2017-02-22 13:09:39 -05:00
< / td >
< / tr >
< tr style = top >
< td >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
### ng-href
2017-10-23 09:47:21 -04:00
2017-05-09 16:01:04 -04:00
< code-example hideCopy format = "" >
2017-10-25 23:27:19 -04:00
< a ng-href="{{ angularDocsUrl }}">Angular Docs< /a>
2017-02-22 13:09:39 -05:00
< / code-example >
2017-03-31 19:57:13 -04:00
The `ng-href` directive allows AngularJS to preprocess the `href` property so that it
can replace the binding expression with the appropriate URL before the browser
fetches from that URL.
In AngularJS, the `ng-href` is often used to activate a route as part of navigation.
2017-10-23 09:47:21 -04:00
2017-05-09 16:01:04 -04:00
< code-example hideCopy format = "" >
2017-10-25 23:27:19 -04:00
< a ng-href="#{{ moviesHash }}">Movies< /a>
2017-02-22 13:09:39 -05:00
< / code-example >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
Routing is handled differently in Angular.
< / td >
< td >
2017-03-31 19:57:13 -04:00
2017-03-31 07:23:16 -04:00
### Bind to the `href` property
2017-03-27 11:08:53 -04:00
2019-07-20 13:40:17 -04:00
< code-example hideCopy path = "ajs-quick-reference/src/app/app.component.html" region = "href" > < / code-example >
2017-03-31 19:57:13 -04:00
Angular uses property binding; there is no built-in *href* directive.
Place the element's `href` property in square brackets and set it to a quoted template expression.
2020-04-28 16:26:58 -04:00
For more information see the [Property binding ](guide/property-binding ) page.
2017-03-31 19:57:13 -04:00
In Angular, `href` is no longer used for routing. Routing uses `routerLink` , as shown in the following example.
2017-03-27 11:08:53 -04:00
2019-07-20 13:40:17 -04:00
< code-example hideCopy path = "ajs-quick-reference/src/app/app.component.html" region = "router-link" > < / code-example >
2017-03-31 19:57:13 -04:00
2020-02-19 17:43:26 -05:00
For more information on routing, see [Defining a basic route ](guide/router#basic-route )
in the [Routing & Navigation ](guide/router ) page.
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
< / td >
< / tr >
< tr style = top >
< td >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
### ng-if
2017-10-23 09:47:21 -04:00
2017-05-09 16:01:04 -04:00
< code-example hideCopy format = "" >
2017-02-22 13:09:39 -05:00
< table ng-if="movies.length">
< / code-example >
2017-03-31 19:57:13 -04:00
In AngularJS, the `ng-if` directive removes or recreates a portion of the DOM,
based on an expression. If the expression is false, the element is removed from the DOM.
In this example, the `<table>` element is removed from the DOM unless the `movies` array has a length greater than zero.
2017-02-22 13:09:39 -05:00
< / td >
< td >
2017-03-31 19:57:13 -04:00
2017-03-31 07:23:16 -04:00
### *ngIf
2017-03-27 11:08:53 -04:00
2019-07-20 13:40:17 -04:00
< code-example hideCopy path = "ajs-quick-reference/src/app/movie-list.component.html" region = "ngIf" > < / code-example >
2017-03-31 19:57:13 -04:00
2017-10-23 09:47:21 -04:00
The `*ngIf` directive in Angular works the same as the `ng-if` directive in AngularJS. It removes
2017-03-31 19:57:13 -04:00
or recreates a portion of the DOM based on an expression.
In this example, the `<table>` element is removed from the DOM unless the `movies` array has a length.
The (*) before `ngIf` is required in this example.
For more information, see [Structural Directives ](guide/structural-directives ).
2017-02-22 13:09:39 -05:00
< / td >
< / tr >
< tr style = top >
< td >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
### ng-model
2017-10-23 09:47:21 -04:00
2017-05-09 16:01:04 -04:00
< code-example hideCopy format = "" >
2017-02-22 13:09:39 -05:00
< input ng-model="vm.favoriteHero"/>
< / code-example >
2017-03-31 19:57:13 -04:00
In AngularJS, the `ng-model` directive binds a form control to a property in the controller associated with the template.
This provides **two-way binding** , whereby any change made to the value in the view is synchronized with the model, and any change to the model is synchronized with the value in the view.
2017-02-22 13:09:39 -05:00
< / td >
< td >
2017-03-31 19:57:13 -04:00
2017-03-31 07:23:16 -04:00
### ngModel
2017-03-27 11:08:53 -04:00
2019-07-20 13:40:17 -04:00
< code-example hideCopy path = "ajs-quick-reference/src/app/movie-list.component.html" region = "ngModel" > < / code-example >
2017-03-31 19:57:13 -04:00
In Angular, **two-way binding** is denoted by `[()]` , descriptively referred to as a "banana in a box". This syntax is a shortcut for defining both property binding (from the component to the view)
and event binding (from the view to the component), thereby providing two-way binding.
2017-10-23 09:47:21 -04:00
For more information on two-way binding with `ngModel` , see the [NgModel— Two-way binding to
2020-04-28 16:26:58 -04:00
form elements with `[(ngModel)]` ](../guide/built-in-directives#ngModel)
section of the [Built-in directives ](guide/built-in-directives ) page.
2017-02-22 13:09:39 -05:00
< / td >
< / tr >
< tr style = top >
< td >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
### ng-repeat
2017-10-23 09:47:21 -04:00
2017-05-09 16:01:04 -04:00
< code-example hideCopy format = "" >
2017-02-22 13:09:39 -05:00
< tr ng-repeat="movie in vm.movies">
< / code-example >
2017-03-31 19:57:13 -04:00
In AngularJS, the `ng-repeat` directive repeats the associated DOM element
for each item in the specified collection.
In this example, the table row (`< tr > `) element repeats for each movie object in the collection of movies.
2017-02-22 13:09:39 -05:00
< / td >
< td >
2017-03-31 19:57:13 -04:00
2017-03-31 07:23:16 -04:00
### *ngFor
2017-03-27 11:08:53 -04:00
2019-07-20 13:40:17 -04:00
< code-example hideCopy path = "ajs-quick-reference/src/app/movie-list.component.html" region = "ngFor" > < / code-example >
2017-03-31 19:57:13 -04:00
2017-10-23 09:47:21 -04:00
The `*ngFor` directive in Angular is similar to the `ng-repeat` directive in AngularJS. It repeats
2017-03-31 19:57:13 -04:00
the associated DOM element for each item in the specified collection.
More accurately, it turns the defined element (`< tr > ` in this example) and its contents into a template and
uses that template to instantiate a view for each item in the list.
Notice the other syntax differences:
The (*) before `ngFor` is required;
the `let` keyword identifies `movie` as an input variable;
the list preposition is `of` , not `in` .
For more information, see [Structural Directives ](guide/structural-directives ).
2017-02-22 13:09:39 -05:00
< / td >
< / tr >
< tr style = top >
< td >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
### ng-show
2017-10-23 09:47:21 -04:00
2017-05-09 16:01:04 -04:00
< code-example hideCopy format = "" >
2017-03-31 19:57:13 -04:00
< h3 ng-show="vm.favoriteHero">
Your favorite hero is: {{vm.favoriteHero}}
< /h3>
2017-02-22 13:09:39 -05:00
< / code-example >
2017-03-31 19:57:13 -04:00
In AngularJS, the `ng-show` directive shows or hides the associated DOM element, based on
an expression.
In this example, the `<div>` element is shown if the `favoriteHero` variable is truthy.
2017-02-22 13:09:39 -05:00
< / td >
< td >
2017-03-31 19:57:13 -04:00
2017-03-31 07:23:16 -04:00
### Bind to the `hidden` property
2017-03-27 11:08:53 -04:00
2019-07-20 13:40:17 -04:00
< code-example hideCopy path = "ajs-quick-reference/src/app/movie-list.component.html" region = "hidden" > < / code-example >
2017-03-31 19:57:13 -04:00
Angular uses property binding; there is no built-in *show* directive.
For hiding and showing elements, bind to the HTML `hidden` property.
To conditionally display an element, place the element's `hidden` property in square brackets and
set it to a quoted template expression that evaluates to the *opposite* of *show* .
In this example, the `<div>` element is hidden if the `favoriteHero` variable is not truthy.
2020-04-28 16:26:58 -04:00
For more information on property binding, see the [Property binding ](guide/property-binding ) page.
2017-02-22 13:09:39 -05:00
< / td >
< / tr >
< tr style = top >
< td >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
### ng-src
2017-10-23 09:47:21 -04:00
2017-05-09 16:01:04 -04:00
< code-example hideCopy format = "" >
2017-02-22 13:09:39 -05:00
< img ng-src="{{movie.imageurl}}">
< / code-example >
2017-03-31 19:57:13 -04:00
The `ng-src` directive allows AngularJS to preprocess the `src` property so that it
can replace the binding expression with the appropriate URL before the browser
fetches from that URL.
2017-02-22 13:09:39 -05:00
< / td >
< td >
2017-03-31 19:57:13 -04:00
2017-03-31 07:23:16 -04:00
### Bind to the `src` property
2017-03-27 11:08:53 -04:00
2019-07-20 13:40:17 -04:00
< code-example hideCopy path = "ajs-quick-reference/src/app/app.component.html" region = "src" > < / code-example >
2017-03-31 19:57:13 -04:00
Angular uses property binding; there is no built-in *src* directive.
Place the `src` property in square brackets and set it to a quoted template expression.
2020-04-28 16:26:58 -04:00
For more information on property binding, see the [Property binding ](guide/property-binding ) page.
2017-02-22 13:09:39 -05:00
< / td >
< / tr >
< tr style = top >
< td >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
### ng-style
2017-10-23 09:47:21 -04:00
2017-05-09 16:01:04 -04:00
< code-example hideCopy format = "" >
2017-02-22 13:09:39 -05:00
< div ng-style="{color: colorPreference}">
< / code-example >
2017-03-31 19:57:13 -04:00
In AngularJS, the `ng-style` directive sets a CSS style on an HTML element
based on an expression. That expression is often a key-value control object with each
key of the object defined as a CSS property, and each value defined as an expression
that evaluates to a value appropriate for the style.
In the example, the `color` style is set to the current value of the `colorPreference` variable.
2017-02-22 13:09:39 -05:00
< / td >
< td >
2017-03-31 19:57:13 -04:00
2017-03-31 07:23:16 -04:00
### ngStyle
2017-03-27 11:08:53 -04:00
2019-07-20 13:40:17 -04:00
< code-example hideCopy path = "ajs-quick-reference/src/app/app.component.html" region = "ngStyle" > < / code-example >
2017-03-31 19:57:13 -04:00
In Angular, the `ngStyle` directive works similarly. It sets a CSS style on an HTML element based on an expression.
In the first example, the `color` style is set to the current value of the `colorPreference` variable.
Angular also has **style binding** , which is good way to set a single style. This is shown in the second example.
2020-04-28 16:26:58 -04:00
For more information on style binding, see the [Style binding ](guide/attribute-binding#style-binding ) section of the
[Attribute binding ](guide/attribute-binding ) page.
2017-03-31 19:57:13 -04:00
2020-04-28 16:26:58 -04:00
For more information on the `ngStyle` directive, see the [NgStyle ](guide/built-in-directives#ngStyle )
section of the [Built-in directives ](guide/built-in-directives ) page.
2017-02-22 13:09:39 -05:00
< / td >
< / tr >
< tr style = top >
< td >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
### ng-switch
2017-10-23 09:47:21 -04:00
2017-05-09 16:01:04 -04:00
< code-example hideCopy format = "" >
2017-03-31 19:57:13 -04:00
< 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>
2017-02-22 13:09:39 -05:00
< / code-example >
2017-03-31 19:57:13 -04:00
In AngularJS, the `ng-switch` directive swaps the contents of
an element by selecting one of the templates based on the current value of an expression.
In this example, if `favoriteHero` is not set, the template displays "Please enter ...".
If `favoriteHero` is set, it checks the movie hero by calling a controller method.
If that method returns `true` , the template displays "Excellent choice!".
If that methods returns `false` , the template displays "No movie, sorry!".
2017-02-22 13:09:39 -05:00
< / td >
< td >
2017-03-31 19:57:13 -04:00
2017-03-31 07:23:16 -04:00
### ngSwitch
2017-03-27 11:08:53 -04:00
2019-07-20 13:40:17 -04:00
< code-example hideCopy path = "ajs-quick-reference/src/app/movie-list.component.html" region = "ngSwitch" > < / code-example >
2017-03-31 19:57:13 -04:00
In Angular, the `ngSwitch` directive works similarly.
It displays an element whose `*ngSwitchCase` matches the current `ngSwitch` expression value.
In this example, if `favoriteHero` is not set, the `ngSwitch` value is `null`
and `*ngSwitchDefault` displays, "Please enter ...".
If `favoriteHero` is set, the app checks the movie hero by calling a component method.
If that method returns `true` , the app selects `*ngSwitchCase="true"` and displays: "Excellent choice!"
If that methods returns `false` , the app selects `*ngSwitchCase="false"` and displays: "No movie, sorry!"
The (*) before `ngSwitchCase` and `ngSwitchDefault` is required in this example.
2020-04-28 16:26:58 -04:00
For more information, see [The NgSwitch directives ](guide/built-in-directives#ngSwitch )
section of the [Built-in directives ](guide/built-in-directives ) page.
2017-02-22 13:09:39 -05:00
< / td >
< / tr >
< / table >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
{@a filters-pipes}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
## Filters/pipes
2017-03-06 05:43:33 -05:00
Angular **pipes** provide formatting and transformation for data in the template, similar to AngularJS **filters** .
2017-02-22 13:09:39 -05:00
Many of the built-in filters in AngularJS have corresponding pipes in Angular.
2017-03-11 10:36:40 -05:00
For more information on pipes, see [Pipes ](guide/pipes ).
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-02-22 13:09:39 -05:00
< table width = "100%" >
< col width = "50%" >
< / col >
< col width = "50%" >
< / col >
< tr >
< th >
AngularJS
< / th >
< th >
Angular
< / th >
< / tr >
< tr style = top >
< td >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
### currency
2017-10-23 09:47:21 -04:00
2017-05-09 16:01:04 -04:00
< code-example hideCopy >
2017-02-22 13:09:39 -05:00
< td>{{movie.price | currency}}< /td>
< / code-example >
2017-03-31 19:57:13 -04:00
2017-03-06 05:43:33 -05:00
Formats a number as currency.
2017-02-22 13:09:39 -05:00
< / td >
< td >
2017-03-31 19:57:13 -04:00
2017-03-31 07:23:16 -04:00
### currency
2017-03-27 11:08:53 -04:00
2019-07-20 13:40:17 -04:00
< code-example hideCopy path = "ajs-quick-reference/src/app/app.component.html" region = "currency" > < / code-example >
2017-03-31 19:57:13 -04:00
2017-03-27 11:08:53 -04:00
The Angular `currency` pipe is similar although some of the parameters have changed.
2017-02-22 13:09:39 -05:00
< / td >
< / tr >
< tr style = top >
< td >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
### date
2017-10-23 09:47:21 -04:00
2017-05-09 16:01:04 -04:00
< code-example hideCopy >
2017-03-27 11:08:53 -04:00
< td>{{movie.releaseDate | date}}< /td>
2017-02-22 13:09:39 -05:00
< / code-example >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
Formats a date to a string based on the requested format.
< / td >
< td >
2017-03-31 19:57:13 -04:00
2017-03-31 07:23:16 -04:00
### date
2017-03-27 11:08:53 -04:00
2019-07-20 13:40:17 -04:00
< code-example hideCopy path = "ajs-quick-reference/src/app/app.component.html" region = "date" > < / code-example >
2017-03-31 19:57:13 -04:00
The Angular `date` pipe is similar.
2017-02-22 13:09:39 -05:00
< / td >
< / tr >
< tr style = top >
< td >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
### filter
2017-10-23 09:47:21 -04:00
2017-05-09 16:01:04 -04:00
< code-example hideCopy >
2017-02-22 13:09:39 -05:00
< tr ng-repeat="movie in movieList | filter: {title:listFilter}">
< / code-example >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
Selects a subset of items from the defined collection, based on the filter criteria.
< / td >
< td >
2017-03-31 19:57:13 -04:00
### none
For 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.
2017-02-22 13:09:39 -05:00
< / td >
< / tr >
< tr style = top >
< td >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
### json
2017-10-23 09:47:21 -04:00
2017-05-09 16:01:04 -04:00
< code-example hideCopy >
2017-02-22 13:09:39 -05:00
< pre>{{movie | json}}< /pre>
< / code-example >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
Converts a JavaScript object into a JSON string. This is useful for debugging.
< / td >
< td >
2017-03-31 19:57:13 -04:00
2017-03-31 07:23:16 -04:00
### json
2017-03-27 11:08:53 -04:00
2019-07-20 13:40:17 -04:00
< code-example hideCopy path = "ajs-quick-reference/src/app/app.component.html" region = "json" > < / code-example >
2017-03-31 19:57:13 -04:00
2017-03-27 11:08:53 -04:00
The Angular `json` pipe does the same thing.
2017-02-22 13:09:39 -05:00
< / td >
< / tr >
< tr style = top >
< td >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
### limitTo
2017-10-23 09:47:21 -04:00
2017-05-09 16:01:04 -04:00
< code-example hideCopy >
2017-02-22 13:09:39 -05:00
< tr ng-repeat="movie in movieList | limitTo:2:0">
< / code-example >
2017-03-31 19:57:13 -04:00
Selects up to the first parameter (2) number of items from the collection
starting (optionally) at the beginning index (0).
2017-02-22 13:09:39 -05:00
< / td >
< td >
2017-03-31 19:57:13 -04:00
2017-03-31 07:23:16 -04:00
### slice
2017-03-27 11:08:53 -04:00
2019-07-20 13:40:17 -04:00
< code-example hideCopy path = "ajs-quick-reference/src/app/app.component.html" region = "slice" > < / code-example >
2017-03-31 19:57:13 -04:00
The `SlicePipe` does the same thing but the *order of the parameters is reversed* , in keeping
with the JavaScript `Slice` method.
The first parameter is the starting index; the second is the limit.
As in AngularJS, coding this operation within the component instead could improve performance.
2017-02-22 13:09:39 -05:00
< / td >
< / tr >
< tr style = top >
< td >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
### lowercase
2017-10-23 09:47:21 -04:00
2017-05-09 16:01:04 -04:00
< code-example hideCopy >
2018-06-20 08:54:32 -04:00
< td>{{movie.title | lowercase}}< /td>
2017-02-22 13:09:39 -05:00
< / code-example >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
Converts the string to lowercase.
< / td >
< td >
2017-03-31 19:57:13 -04:00
2017-03-31 07:23:16 -04:00
### lowercase
2017-03-27 11:08:53 -04:00
2019-07-20 13:40:17 -04:00
< code-example hideCopy path = "ajs-quick-reference/src/app/app.component.html" region = "lowercase" > < / code-example >
2017-03-31 19:57:13 -04:00
2017-03-27 11:08:53 -04:00
The Angular `lowercase` pipe does the same thing.
2017-02-22 13:09:39 -05:00
< / td >
< / tr >
< tr style = top >
< td >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
### number
2017-05-09 16:01:04 -04:00
< code-example hideCopy >
2017-03-27 11:08:53 -04:00
< td>{{movie.starRating | number}}< /td>
2017-02-22 13:09:39 -05:00
< / code-example >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
Formats a number as text.
< / td >
< td >
2017-03-31 19:57:13 -04:00
2017-03-31 07:23:16 -04:00
### number
2017-03-27 11:08:53 -04:00
2019-07-20 13:40:17 -04:00
< code-example hideCopy path = "ajs-quick-reference/src/app/app.component.html" region = "number" > < / code-example >
2017-03-31 19:57:13 -04:00
The Angular `number` pipe is similar.
It provides more functionality when defining
the decimal places, as shown in the second example above.
Angular also has a `percent` pipe, which formats a number as a local percentage
as shown in the third example.
2017-02-22 13:09:39 -05:00
< / td >
< / tr >
< tr style = top >
< td >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
### orderBy
2017-10-23 09:47:21 -04:00
2017-05-09 16:01:04 -04:00
< code-example hideCopy >
2017-02-22 13:09:39 -05:00
< tr ng-repeat="movie in movieList | orderBy : 'title'">
< / code-example >
2017-03-31 19:57:13 -04:00
Displays the collection in the order specified by the expression.
In this example, the movie title orders the `movieList` .
2017-02-22 13:09:39 -05:00
< / td >
< td >
2017-03-31 19:57:13 -04:00
### none
For 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.
2017-02-22 13:09:39 -05:00
< / td >
< / tr >
< / table >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
{@a controllers-components}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
## Modules/controllers/components
2017-07-11 18:05:20 -04:00
In both AngularJS and Angular, modules help you organize your application into cohesive blocks of functionality.
2017-02-22 13:09:39 -05:00
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.
2017-03-30 15:04:18 -04:00
2017-02-22 13:09:39 -05:00
< table width = "100%" >
< col width = "50%" >
< / col >
< col width = "50%" >
< / col >
< tr >
< th >
AngularJS
< / th >
< th >
Angular
< / th >
< / tr >
< tr style = top >
< td >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
### IIFE
2017-10-23 09:47:21 -04:00
2017-05-09 16:01:04 -04:00
< code-example hideCopy >
2017-03-31 19:57:13 -04:00
(function () {
...
}());
2017-02-22 13:09:39 -05:00
< / code-example >
2017-03-31 19:57:13 -04:00
2017-10-23 09:47:21 -04:00
In AngularJS, an immediately invoked function expression (or IIFE) around controller code
2017-03-31 19:57:13 -04:00
keeps it out of the global namespace.
2017-02-22 13:09:39 -05:00
< / td >
< td >
2017-03-31 19:57:13 -04:00
### none
2017-10-23 09:47:21 -04:00
This is a nonissue in Angular because ES 2015 modules
2017-03-31 19:57:13 -04:00
handle the namespacing for you.
2017-10-23 09:47:21 -04:00
For more information on modules, see the [Modules ](guide/architecture#modules ) section of the
2017-03-31 19:57:13 -04:00
[Architecture Overview ](guide/architecture ).
2017-02-22 13:09:39 -05:00
< / td >
< / tr >
< tr style = top >
< td >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
### Angular modules
2017-10-23 09:47:21 -04:00
2017-05-09 16:01:04 -04:00
< code-example hideCopy >
2017-02-22 13:09:39 -05:00
angular.module("movieHunter", ["ngRoute"]);
< / code-example >
2017-03-31 19:57:13 -04:00
2017-10-23 09:47:21 -04:00
In AngularJS, an Angular module keeps track of controllers, services, and other code.
2017-03-31 19:57:13 -04:00
The second argument defines the list of other modules that this module depends upon.
2017-02-22 13:09:39 -05:00
< / td >
< td >
2017-03-31 19:57:13 -04:00
2017-07-11 18:05:20 -04:00
### NgModules
2017-03-27 11:08:53 -04:00
2019-07-20 13:40:17 -04:00
< code-example hideCopy path = "ajs-quick-reference/src/app/app.module.1.ts" > < / code-example >
2017-03-31 19:57:13 -04:00
2017-07-11 18:05:20 -04:00
NgModules, defined with the `NgModule` decorator, serve the same purpose:
2017-03-31 19:57:13 -04:00
* `imports` : specifies the list of other modules that this module depends upon
* `declaration` : keeps track of your components, pipes, and directives.
2017-07-04 10:58:20 -04:00
For more information on modules, see [NgModules ](guide/ngmodules ).
2017-02-22 13:09:39 -05:00
< / td >
< / tr >
< tr style = top >
< td >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
### Controller registration
2017-10-23 09:47:21 -04:00
2017-05-09 16:01:04 -04:00
< code-example hideCopy >
2017-03-31 19:57:13 -04:00
angular
.module("movieHunter")
.controller("MovieListCtrl",
["movieService",
MovieListCtrl]);
2017-02-22 13:09:39 -05:00
< / code-example >
2017-03-31 19:57:13 -04:00
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.
2017-02-22 13:09:39 -05:00
< / td >
< td >
2017-03-31 19:57:13 -04:00
2017-03-31 07:23:16 -04:00
### Component decorator
2017-03-27 11:08:53 -04:00
2019-07-20 13:40:17 -04:00
< code-example hideCopy path = "ajs-quick-reference/src/app/movie-list.component.ts" region = "component" > < / code-example >
2017-03-31 19:57:13 -04:00
Angular adds a decorator to the component class to provide any required metadata.
The `@Component` decorator declares that the class is a component and provides metadata about
that component such as its selector (or tag) and its template.
This is how you associate a template with logic, which is defined in the component class.
2017-10-23 09:47:21 -04:00
For more information, see the [Components ](guide/architecture#components )
2017-03-31 19:57:13 -04:00
section of the [Architecture Overview ](guide/architecture ) page.
2017-02-22 13:09:39 -05:00
< / td >
< / tr >
< tr style = top >
< td >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
### Controller function
2017-10-23 09:47:21 -04:00
2017-05-09 16:01:04 -04:00
< code-example hideCopy >
2017-03-31 19:57:13 -04:00
function MovieListCtrl(movieService) {
}
2017-02-22 13:09:39 -05:00
< / code-example >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
In AngularJS, you write the code for the model and methods in a controller function.
< / td >
< td >
2017-03-31 19:57:13 -04:00
2017-03-31 07:23:16 -04:00
### Component class
2017-03-27 11:08:53 -04:00
2019-07-20 13:40:17 -04:00
< code-example hideCopy path = "ajs-quick-reference/src/app/movie-list.component.ts" region = "class" > < / code-example >
2017-03-31 19:57:13 -04:00
2019-10-02 14:28:12 -04:00
In Angular, you create a component class to contain the data model and control methods. Use the TypeScript < code > export< / code > keyword to export the class so that the functionality can be imported into NgModules.
2017-03-31 19:57:13 -04:00
2017-10-23 09:47:21 -04:00
For more information, see the [Components ](guide/architecture#components )
2017-03-31 19:57:13 -04:00
section of the [Architecture Overview ](guide/architecture ) page.
2017-02-22 13:09:39 -05:00
< / td >
< / tr >
< tr style = top >
< td >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
### Dependency injection
2017-10-23 09:47:21 -04:00
2017-05-09 16:01:04 -04:00
< code-example hideCopy >
2017-03-31 19:57:13 -04:00
MovieListCtrl.$inject = ['MovieService'];
function MovieListCtrl(movieService) {
}
2017-02-22 13:09:39 -05:00
< / code-example >
2017-03-31 19:57:13 -04:00
In AngularJS, you pass in any dependencies as controller function arguments.
This example injects a `MovieService` .
To guard against minification problems, tell Angular explicitly
that it should inject an instance of the `MovieService` in the first parameter.
2017-02-22 13:09:39 -05:00
< / td >
< td >
2017-03-31 19:57:13 -04:00
2017-03-31 07:23:16 -04:00
### Dependency injection
2017-03-27 11:08:53 -04:00
2019-07-20 13:40:17 -04:00
< code-example hideCopy path = "ajs-quick-reference/src/app/movie-list.component.ts" region = "di" > < / code-example >
2017-03-31 19:57:13 -04:00
In Angular, you pass in dependencies as arguments to the component class constructor.
This example injects a `MovieService` .
The first parameter's TypeScript type tells Angular what to inject, even after minification.
2017-10-23 09:47:21 -04:00
For more information, see the [Dependency injection ](guide/architecture#dependency-injection )
2017-03-31 19:57:13 -04:00
section of the [Architecture Overview ](guide/architecture ).
2017-02-22 13:09:39 -05:00
< / td >
< / tr >
< / table >
{@a style-sheets}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
## 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.
2017-03-30 15:04:18 -04:00
2017-02-22 13:09:39 -05:00
< table width = "100%" >
< col width = "50%" >
< / col >
< col width = "50%" >
< / col >
< tr >
< th >
AngularJS
< / th >
< th >
Angular
< / th >
< / tr >
< tr style = top >
< td >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
### Link tag
2017-10-23 09:47:21 -04:00
2017-05-09 16:01:04 -04:00
< code-example hideCopy >
2017-02-22 13:09:39 -05:00
< link href="styles.css" rel="stylesheet" />
< / code-example >
2017-03-31 19:57:13 -04:00
AngularJS, uses a `link` tag in the head section of the `index.html` file
to define the styles for the application.
2017-02-22 13:09:39 -05:00
< / td >
< td >
2017-03-31 19:57:13 -04:00
2017-10-04 07:48:04 -04:00
### Styles configuration
2019-07-20 13:40:17 -04:00
< code-example hideCopy path = "ajs-quick-reference/.angular-cli.1.json" region = "styles" > < / code-example >
2017-03-31 19:57:13 -04:00
2018-04-30 08:23:24 -04:00
With the Angular CLI, you can configure your global styles in the `angular.json` file.
2017-10-04 07:48:04 -04:00
You can rename the extension to `.scss` to use sass.
2017-03-31 19:57:13 -04:00
### StyleUrls
In Angular, you can use the `styles` or `styleUrls` property of the `@Component` metadata to define
a style sheet for a particular component.
2017-03-27 11:08:53 -04:00
2019-07-20 13:40:17 -04:00
< code-example hideCopy path = "ajs-quick-reference/src/app/movie-list.component.ts" region = "style-url" > < / code-example >
2017-03-31 19:57:13 -04:00
This allows you to set appropriate styles for individual components that won’ t leak into
other parts of the application.
2017-02-22 13:09:39 -05:00
< / td >
< / tr >
< / table >