开发学院

您的位置:首页>教程>正文

教程正文

Angular 6 数据绑定

  数据绑定是Angular体系中重要的功能,一般使用大括号{{}}进行数据绑定,这个过程被称为插值。我们已经在前面的例子中看到了我们是如何声明变量title的值的,并且同样的值会打印在浏览器中。

app.component.ts文件中的变量称为{{title}},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 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中的语法是*ngFor = "let I of months",为了获得月数,我们将在{{i}}中显示它。

  这两个大括号有助于数据绑定,你在app.component.ts文件中声明变量,同样的变量将被用大括号替换。

  让我们看一下浏览器的输出:

output_months_array_in_browser.jpg

  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.jpg

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

  在这种情况下,我们将可利用变量设为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>

  如果与else条件一起使用,并且使用的变量是条件1。相同的内容被指定为ng-template的id,当可用变量设置为false时,文本条件无效。

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

output_using_if_else_condition.jpg

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

  现在,我们将使变量变为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的模板。

  浏览器中的显示如下所示:

output_using_if_statement_2.jpg