Angular 6 - 数据绑定



数据绑定从 AngularJS、Angular 2、4 开始就可用,现在 Angular 6 也可用。我们使用花括号进行数据绑定 - {{}}; 此过程称为插值。我们已经在之前的示例中看到过如何将值声明为变量 title,并在浏览器中打印出来。

app.component.html 文件中的变量被称为 {{title}},title 的值在 app.component.ts 文件中初始化,并在 app.component.html 中显示其值。

现在让我们在浏览器中创建一个月份下拉列表。为此,我们在 app.component.ts 中创建了一个月份数组,如下所示:

import { Component } from '@angular/core';
@Component({
   selector: 'app-root',
   templateUrl: './app.component.html',
   styleUrls: ['./app.component.css']
})
export class AppComponent {
   title = 'Angular 6 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>

我们创建了带有 option 的普通 select 标签。在 option 中,我们使用了 for 循环for 循环 用于迭代月份数组,这将依次创建带有月份中值的 option 标签。

Angular 中的语法 for*ngFor = "let i of months",要获取月份的值,我们将其显示在 {{i}} 中。

两个花括号有助于数据绑定。您在 app.component.ts 文件中声明变量,然后使用花括号替换它们。

让我们看看上面月份数组在浏览器中的输出。

Output Month's Array in Browser

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>

输出

Output Using IF-Statement

让我们使用 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-template,如下所示:

<ng-template #condition1>Condition is invalid</ng-template>

完整的代码如下:

<!--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>

If 用于 else 条件,使用的变量是 condition1。它被分配为 ng-templateid,当 available 变量设置为 false 时,将显示文本 Condition is invalid

以下屏幕截图显示了浏览器中的显示。

Output Using If-Else Condition

现在让我们使用 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;
}

现在,我们将变量 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>

如果变量为 true,则为 condition1,否则为 condition2。现在,创建了两个带有 id #condition1#condition2 的模板。

浏览器中的显示如下:

Output Using IF-Statement
广告
© . All rights reserved.