Professional Documents
Culture Documents
Unit V
Unit V
ANGULAR EXPRESSIONS,DIRECTIVES
AND DATA BINDING
EXPRESSIONS
• A great feature of Angular is the capability to
add JavaScript-like expressions inside an HTML
template.
• Angular evaluates expressions and then can
dynamically add the results to a web page.
• Expressions are linked to a component, and
you can have an expression that utilizes values
in the component, and its value can change as
the model changes.
EXPRESSION REPRESENTATION
{{expression}}
The Angular compiler compiles an expression into HTML
elements so that the results of the expression are displayed.
For example, look at the following expressions:
{{1+5}}
{{'One' + 'Two'}}
Based on these expressions, the web page displays the
following values:
6
OneTwo
REFERING THE COMPONENT VALUE
• example, say that a component contains the
following values:
• name: string='Brad';
• score: number=95;
• You can directly reference the name and score
values in the template expressions, as shown
here:
• Name: {{name}}
• Score: {{score}}
ANGULAR EXPRESSIONS
Angular expressions are similar to TypeScript/JavaScript expressions in several
ways, but they differ in these ways:
Attribute evaluation: Property names are evaluated against the component
model instead of against the global JavaScript namespace.
More forgiving: Expressions do not throw exceptions when they encounter
undefined or null variable types; instead, they treat them as having no value.
No flow control: Expressions do not allow the following:
Assignments (for example, =, +=, -=)
The new operator
Conditionals
Loops
Increment and decrement operators (++ and --)
Also, you cannot throw an error inside an expression
Using Angular expressions that contain
strings, numbers, and basic math
operations
basicExpressions.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
template: `
<h1>Expressions</h1>
Number:<br>
{{5}}<hr>
String:<br>
{{'My String'}}<hr>
basicExpressions.component.ts
color: {{optionColor.value}}
</span><br>
attribute.component.css
: A CSS File That Styles the
Application
Click here to view code image
.bold {
font-weight: bold;
font-style: italic;
}
highlight {
background-color: lightblue;
•
OUTPUT
Data Binding
• Data binding is the process of linking data
from a component with what is displayed in a
web page. When data in the component
changes, the UI rendered to the user is
automatically updated.
Types of DataBinding
• The following is a list of the types of data binding
available with Angular that are discussed in this chapter:
• Interpolation: You can use double curly braces ({{}}) to
get values directly from the Component class.
• Property binding: You can use this type of binding to set
the property of an HTML element.
• Event binding: You can use this type of binding to
handle user inputs.
• Attribute binding: This type of binding allows the setting
of attributes to an HTML element.
Types of Data Binding
• Class binding: You can use this type of binding
to set CSS class names to the element.
• Style binding: You can use this type of binding
to create inline CSS styles for the element.
Two-way binding with ngModel: You can use
this type of binding with data entry forms to
receive and display data.
Interpolation
• Interpolation involves using the {{}} double
curly braces to evaluate a template
expression. This can be in a hard-coded form,
or it can reference a property of the
Component class.
• Here is an example of the syntax to do this..,
• <img src="{{imgUrl}}"/>
Interpolation -EXample
•import { Component } from '@angular/core';
•02
•03 @Component({
•04 selector: 'app-root',
•05 template: `
•06 {{str1 + ' ' + name}}
•07 <br>
•08 <img src="{{imageSrc}}" />
•09 <br>
•10 <p>{{str2 + getLikes(likes)}}</p>
•11 `,
•12 styles: [`
•13 img{
•14 width: 300px;
•15 height: auto;
•16 }
•17 p{
•18 font-size: 35px;
•19 color: darkBlue;
•20 }
•21 `]
•22 })
•23 export class AppComponent {
•24 str1: string = "Hello my name is"
•25 name: string = "Brendan"
•26 str2: string = "I like to"
•27 likes: string[] = ['hike', "rappel", "Jeep"]
•28 getLikes = function(arr: any){
•29 var arrString = arr.join(", ");
•30 return " " + arrString
•31 }
•32 imageSrc: string = "../assets/images/angelsLanding.jpg"
Property Binding
• Property Binding
• You use property binding when you need to
set the property of an HTML element.
• You do this by defining the value you want
within the Component class. Then you
• bind that value to the component template,
using the following syntax:
• <img [src]="myValue">
Property Binding -Example
• property.component.ts: Property Binding with Logic and the
• Application of a Class Name
• Click here to view code image
• 01 import { Component } from '@angular/core';
• 02
• 03 @Component({
• 04 selector: 'app-root',
• 05 template: `
• 06 <img [src]="myPic"/>
• 07 <br>
• 08 <button [disabled]="isEnabled">Click me</button><hr>
• 09 <button disabled="{!isEnabled}">Click me</button><br>
• 10 <p [ngClass]="className">This is cool stuff</p>
• 11 `,
• 12 styles: [`
Property Binding -Example
• 13 img {
• 14 height: 100px;
• 15 width auto;
• 16 }
• 17 .myClass {
• 18 color: red;
• 19 font-size: 24px;
• 20 }
• 21 `]
• 22 })
• 23 export class AppComponent {
• 24 myPic: string = "../assets/images/sunset.JPG";
• 25 isEnabled: boolean = false;
• 26 className: string = "myClass";
• 27 }
Attribute Binding