Angular 4 数据绑定
Angular 4 数据绑定
数据绑定可以从AngularJS, Angular 2 获得,现在也可以从Angular 4获得。我们使用大括号进行数据绑定-{{}};这个过程被称为插值。我们已经在前面例子看到我们是如何声明变量title的值,并且同样的值会打印在浏览器中。
app.component.html文件中的变量称为 {{title}} ,title值在app.component.ts 文件中初始化,在app.component.html中显示出来。
现在让我们在浏览器中创建一个月份的下拉列表。为此,我们在应用程序组件中创建了一系列月份,如下所示
import { Component } from '@angular/core'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { title = 'Angular 4 Project!'; // declared array of months. months = ["January", "Feburary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; }
上面的月份数组将显示在浏览器的下拉列表中。为此,我们将使用以下代码:
<!--The content below is only a placeholder and can be replaced. --> <div style="text-align:center"> <h1> Welcome to {{title}}. </h1> </div> <div> Months : <select> <option *ngFor="let i of months">{{i}}</option> </select> </div>
我们已经创建了带有选项的下拉菜单。我们使用for循环,for循环用于迭代月份数组,这反过来将创建月份中存在的值的选项标记。
Angular中的语法是*ngFor = “let I of months,为了获得月数,我们将在{{i}}中显示它。
这两个大括号有助于数据绑定。你在你的应用程序组件文件中声明变量,同样的变量将被用大括号替换。
app.component.ts中设置的变量可以使用{{}}与app.component.html绑定。
现在让我们根据条件在浏览器中显示数据。这里,我们添加了一个变量,并将该值赋值为true。使用if语句,我们可以隐藏/显示要显示的内容。
例子
import { Component } from '@angular/core'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { title = 'Angular 4 Project!'; //array of months. months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; isavailable = true; //variable is set to true } <!--The content below is only a placeholder and can be replaced.--> <div style = "text-align:center"> <h1> Welcome to {{title}}. </h1> </div> <div> Months : <select> <option *ngFor = "let i of months">{{i}}</option> </select> </div> <br/> <div> <span *ngIf = "isavailable">Condition is valid.</span> //over here based on if condition the text condition is valid is displayed. If the value of isavailable is set to false it will not display the text. </div>
输出
让我们使用IF THEN ELSE条件来尝试上面的例子。
例子
import { Component } from '@angular/core'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { title = 'Angular 4 Project!'; //array of months. months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; isavailable = false; }
在这种情况下,我们将isavailable变量设为false。要打印else条件,我们必须按如下方式创建ng模板
<ng-template #condition1>Condition is invalid</ng-template> The full code looks like this − <!--The content below is only a placeholder and can be replaced.--> <div style="text-align:center"> <h1> Welcome to {{title}}. </h1> </div> <div> Months : <select> <option *ngFor="let i of months">{{i}}</option> </select> </div> <br/> <div> <span *ngIf="isavailable; else condition1">Condition is valid.</span> <ng-template #condition1>Condition is invalid</ng-template> </div>
如果与else条件一起使用,并且使用的变量是条件1。相同的内容被指定为ng模板的id,当available变量设置为false时,文本条件无效。
以下截图显示了浏览器中的显示。
现在让我们使用if then else条件。
import { Component } from '@angular/core'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { title = 'Angular 4 Project!'; //array of months. months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; isavailable = true; }
现在,我们将使变量变为真。在html中,条件是以以下方式编写的
<!--The content below is only a placeholder and can be replaced.--> <div style="text-align:center"> <h1> Welcome to {{title}}. </h1> </div> <div> Months : <select> <option *ngFor="let i of months">{{i}}</option> </select> </div> <br/> <div> <span *ngIf="isavailable; then condition1 else condition2">Condition is valid.</span> <ng-template #condition1>Condition is valid</ng-template> <ng-template #condition2>Condition is invalid</ng-template> </div>
如果变量为真,则条件1,否则条件2。现在,创建了两个id为#condition1和#condition2的模板。
浏览器中的显示如下所示: