Angular 4 - 数据绑定



数据绑定从 AngularJS、Angular 2 开始就可用,现在 Angular 4 也可用。我们使用花括号进行数据绑定 - {{}}; 此过程称为插值。我们已经在之前的示例中看到如何将值声明为变量 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 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 中的语法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。将其作为id分配给ng-template,当 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-Then-Else Condition
广告