` element is shown if the `favoriteHero` variable is truthy. |
Angular uses property binding. Angular has no built-in *show* directive. For hiding and showing elements, bind to the HTML `hidden` property.
To conditionally display an element the `hidden` property of the element can be used. Place the `hidden` property in square brackets and set it to a quoted template expression that evaluates to the *opposite* of *show*.
` element is hidden if the `favoriteHero` variable is not truthy.
For more information on property binding, see the [Property binding][AioGuidePropertyBinding] page. |
### `ng-src` → Bind to the `src` property
| AngularJS | Angular |
|:--- |:--- |
|
<img ng-src="{{movie.imageurl}}"> The `ng-src` directive allows AngularJS to preprocess the `src` property. This replaces the binding expression with the appropriate URL before the browser fetches from that URL. |
Angular uses property binding. Angular has no built-in *src* directive. Place the `src` property in square brackets and set it to a quoted template expression.
For more information on property binding, see the [Property binding][AioGuidePropertyBinding] page. |
### `ng-style` → `ngStyle`
| AngularJS | Angular |
|:--- |:--- |
|
<div ng-style="{color: colorPreference}"> 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
- 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. |
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.
For more information on style binding, see the [Style binding][AioGuideAttributeBindingBindingToTheStyleAttribute] section of the [Attribute binding][AioGuideAttributeBinding] page.
For more information on the `ngStyle` directive, see the [NgStyle][AioGuideBuiltInDirectivesSettingInlineStylesWithNgstyle] section of the [Built-in directives][AioGuideBuiltInDirectives] page. |
### `ng-switch` → `ngSwitch`
| AngularJS | Angular |
|:--- |:--- |
|
<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 `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!" |
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 your favorite hero." If `favoriteHero` is set, the application checks the movie hero by calling a component method. If that method returns `true`, the application selects `*ngSwitchCase="true"` and displays: "Excellent choice." If that methods returns `false`, the application selects `*ngSwitchCase="false"` and displays: "No movie, sorry."
The \(`*`\) before `ngSwitchCase` and `ngSwitchDefault` is required in this example.
For more information, see [The NgSwitch directives][AioGuideBuiltInDirectivesSwitchingCasesWithNgswitch] section of the [Built-in directives][AioGuideBuiltInDirectives] page. |
## Filters / pipes
Angular **pipes** provide formatting and transformation for data in the template, like AngularJS **filters**.
Many of the built-in filters in AngularJS have corresponding pipes in Angular.
For more information on pipes, see [Pipes][AioGuidePipes].
### `currency` → `currency`
| AngularJS | Angular |
|:--- |:--- |
|
<td> 
 {{movie.price | currency}} 
 </td> Formats a number as currency. |
The Angular `currency` pipe is similar although some of the parameters have changed. |
### `date` → `date`
| AngularJS | Angular |
|:--- |:--- |
|
<td> 
 {{movie.releaseDate | date}} 
 </td> Formats a date to a string based on the requested format. |
The Angular `date` pipe is similar. |
### `filter` → none
| AngularJS | Angular |
|:--- |:--- |
|
<tr ng-repeat="movie in movieList | filter: {title:listFilter}"> Selects a subset of items from the defined collection, based on the filter criteria. |
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. |
### `json` → `json`
| AngularJS | Angular |
|:--- |:--- |
|
<pre> 
 {{movie | json}} 
 </pre> Converts a JavaScript object into a JSON string. This is useful for debugging. |
The Angular [`json`][AioApiCommonJsonpipe] pipe does the same thing. |
### `limitTo` → `slice`
| AngularJS | Angular |
|:--- |:--- |
|
<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`. |
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 and the second is the limit. As in AngularJS, coding this operation within the component instead could improve performance. |
### `lowercase` → `lowercase`
| AngularJS | Angular |
|:--- |:--- |
|
<td> 
 {{movie.title | lowercase}} 
 </td> Converts the string to lowercase. |
The Angular `lowercase` pipe does the same thing. |
### `number` → `number`
| AngularJS | Angular |
|:--- |:--- |
|
<td> 
 {{movie.starRating | number}} 
 </td> Formats a number as text. |
The Angular [`number`][AioApiCommonDecimalpipe] pipe is similar. It provides more capabilities when defining the decimal places, as shown in the preceding second example.
Angular also has a `percent` pipe, which formats a number as a local percentage as shown in the third example. |
### `orderBy` → none
| AngularJS | Angular |
|:--- |:--- |
|
<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 `movieList`. |
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. |
## Controllers and Components
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** which typically acquires its model from an **injected service**.
Because much AngularJS code is in JavaScript, JavaScript code is shown in the AngularJS column.
The Angular code is shown using TypeScript.
### Immediately invoked function expression (IIFE) → none
| AngularJS | Angular |
|:--- |:--- |
|
( 
 function () { 
 … 
 }() 
); In AngularJS, an IIFE around controller code keeps it out of the global namespace. |
This is a nonissue in Angular because ES 2015 modules handle the namespace for you. |
### Controller registration → component decorator
| AngularJS | Angular |
|:--- |:--- |
|
angular .module( 
 "movieHunter" 
) .controller( 
 "MovieListCtrl", 
 [ 
 "movieService", 
 MovieListCtrl 
 ] 
); AngularJS has code in each controller that looks up an appropriate AngularJS 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. |
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.
For more information, see the [Components][AioGuideArchitectureComponents] section of the [Architecture Overview][AioGuideArchitecture] page. |
### Controller function → component class
| AngularJS | Angular |
|:--- |:--- |
|
function MovieListCtrl(movieService) { 
 } In AngularJS, you write the code for the model and methods in a controller function. |
In Angular, you create a component class to contain the data model and control methods. Use the TypeScript
export keyword to export the class so that the component can be imported into other classes.
For more information, see the [Components][AioGuideArchitectureComponents] section of the [Architecture Overview][AioGuideArchitecture] page. |
### Dependency injection → dependency injection
| AngularJS | Angular |
|:--- |:--- |
|
MovieListCtrl.$inject = [ 
 'MovieService' 
]; 
function MovieListCtrl(movieService) { 
} 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. |
In Angular, you pass in dependencies as arguments to the component class constructor. This example injects a `MovieService`. The TypeScript type of the first parameter tells Angular what to inject, even after minification.
For more information, see the [Dependency injection][AioGuideArchitectureServicesAndDependencyInjection] section of the [Architecture Overview][AioGuideArchitecture]. |
## 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.
Now you can also encapsulate a style sheet within a specific component.
### `Link` tag → `styles` configuration or `styleUrls`
| AngularJS | Angular |
|:--- |:--- |
|
<link href="styles.css" 
 rel="stylesheet" /> AngularJS, uses a `link` tag in the head section of the `index.html` file to define the styles for the application. |
With the Angular CLI, you can configure your global styles in the `angular.json` file. You can rename the extension to `.scss` to use sass.
In Angular, you can use the `styles` or `styleUrls` property of the `@Component` metadata to define a style sheet for a particular component.
This allows you to set appropriate styles for individual components that do not leak into other parts of the application. |
[AioApiCommonDecimalpipe]: api/common/DecimalPipe "DecimalPipe | @angular/common - API | Angular"
[AioApiCommonJsonpipe]: api/common/JsonPipe "JsonPipe | @angular/common - API | Angular"
[AioGuideAjsQuickReferenceFiltersPipes]: guide/ajs-quick-reference#filters--pipes "Filters/pipes - AngularJS to Angular concepts: Quick reference | Angular"
[AioGuideAjsQuickReferenceTemplateDirectives]: guide/ajs-quick-reference#template-directives "Template directives - AngularJS to Angular concepts: Quick reference | Angular"
[AioGuideArchitecture]: guide/architecture "Introduction to Angular concepts | Angular"
[AioGuideArchitectureComponents]: guide/architecture#components "Components - Introduction to Angular concepts | Angular"
[AioGuideArchitectureServicesAndDependencyInjection]: guide/architecture#services-and-dependency-injection "Services and dependency injection - Introduction to Angular concepts | Angular"
[AioGuideAttributeBinding]: guide/attribute-binding "Attribute, class, and style bindings | Angular"
[AioGuideAttributeBindingBindingToTheStyleAttribute]: guide/class-binding "Class and style binding | Angular"
[AioGuideBuiltInDirectives]: guide/built-in-directives "Built-in directives | Angular"
[AioGuideBuiltInDirectivesDisplayingAndUpdatingPropertiesWithNgmodel]: guide/built-in-directives#displaying-and-updating-properties-with-ngmodel "Displaying and updating properties with ngModel - Built-in directives | Angular"
[AioGuideBuiltInDirectivesSettingInlineStylesWithNgstyle]: guide/built-in-directives#setting-inline-styles-with-ngstyle "Setting inline styles with NgStyle - Built-in directives | Angular"
[AioGuideBuiltInDirectivesSwitchingCasesWithNgswitch]: guide/built-in-directives#switching-cases-with-ngswitch "Switching cases with NgSwitch - Built-in directives | Angular"
[AioGuideEventBinding]: guide/event-binding "Event binding | Angular"
[AioGuideInterpolation]: guide/interpolation "Text interpolation | Angular"
[AioGuidePipes]: guide/pipes "Transforming Data Using Pipes | Angular"
[AioGuidePropertyBinding]: guide/property-binding "Property binding | Angular"
[AioGuideRouter]: guide/router "Common Routing Tasks | Angular"
[AioGuideRouterDefiningABasicRoute]: guide/router#defining-a-basic-route "Defining a basic route - Common Routing Tasks | Angular"
[AioGuideStructuralDirectives]: guide/structural-directives "Writing structural directives | Angular"
[AioGuideStructuralDirectivesStructuralDirectiveShorthand]: guide/structural-directives#structural-directive-shorthand "Structural directive shorthand - Writing structural directives | Angular"
[MdnDocsWebEvents]: https://developer.mozilla.org/docs/Web/Events "Event reference | MDN"
@reviewed 2023-09-25