目录

Angular 4 – Data Binding

数据绑定可以从AngularJS,Angular 2获得,现在也可以在Angular 4中使用。 我们使用花括号进行数据绑定 - {{}}; 这个过程叫做插值。 我们在前面的例子中已经看到我们如何向变量标题声明值,并在浏览器中打印相同的值。

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

现在让我们在浏览器中创建一个月下拉列表。 为此,我们在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 loopfor loop用于遍历月份数组,这反过来将创建具有月份中存在的值的选项标记。

Angular的语法是*ngFor = “let I of months”并获得我们在{{i}}中显示月份的值。

两个花括号有助于数据绑定。 您在app.component.ts文件中声明变量,并使用大括号替换相同的变量。

让我们在浏览器中看到上个月数组的输出

在浏览器中输出月份的数组

app.component.ts设置的变量可以使用大括号与app.component.html绑定; 例如, {{}}

现在让我们根据条件在浏览器中显示数据。 在这里,我们添加了一个变量并将值赋值为true。 使用if语句,我们可以隐藏/显示要显示的内容。

例子 (Example)

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)

使用IF语句输出

让我们使用IF THEN ELSE条件尝试上面的例子。

例子 (Example)

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 ,当可用变量设置为false时,将显示文本Condition is invalid

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

使用If-Else条件输出

现在让我们使用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创建两个模板。

浏览器中的显示如下 -

使用If-Then-Else条件输出
↑回到顶部↑
WIKI教程 @2018