码迷,mamicode.com
首页 > 其他好文 > 详细

Anauglar2中的模板语法

时间:2016-05-08 13:45:11      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:

Property 绑定或插入

<img src="{{heroImageUrl}}">
<img [src]="‘‘ + heroImageUrl">

<div>The title is {{title}}</div>
<div [textContent]="‘The title is ‘+title"></div>

 

Attribute, Class, and Style 绑定

1.Attribute Bingding
<td [attr.colspan]="1 + 1">One-Two</td>

<button [attr.aria-label]="actionName">{{actionName}} with Aria</button>

 

2.Class Bingding
<div class="bad curly special"   [class]="‘bad curly‘">Bad curly</div>

<div [class.special]="isSpecial">The class binding is special</div>

<div class="special"  [class.special]="!isSpecial">This one is not so special</div>

 

3.Style Binding
<button [style.color] = "isSpecial ? ‘red‘ : ‘green‘">Red</button>
<button [style.backgroundColor]="canSave ?‘cyan‘ : ‘grey‘" >Save</button>

<button [style.fontSize.em]="isSpecial ? 3 : 1" >Big</button>
<button [style.fontSize.%]="!isSpecial ? 150 : 50" >Small</button>

 

4.Event Binding

<button (click)="onSave()">Save</button>

<button on-click="onSave()">On Save</button>

!-- `myClick` is an event on the custom `MyClickDirective` -->
<div myClick (myClick)="clickity=$event">click with myClick</div>


<input [value]="currentHero.firstName"  (input)="currentHero.firstName=$event.target.value" >

 

NgClass

setClasses() {
  let classes =  {
    saveable: this.canSave,      // true
    modified: !this.isUnchanged, // false
    special: this.isSpecial,     // true
  }
  return classes;
}

<div [ngClass]="setClasses()">This div is saveable and special</div>

 

NgStyle

setStyles() {
  let styles = {
    // CSS property names
    font-style:  this.canSave      ? italic : normal,  // italic
    font-weight: !this.isUnchanged ? bold   : normal,  // normal
    font-size:   this.isSpecial    ? x-large: smaller, // larger
  }
  return styles;
}


<div [ngStyle]="setStyles()">
  This div is italic, normal weight, and x-large
</div>

 

NgIf

<div *ngIf="currentHero">Hello, {{currentHero.firstName}}</div>

 

NgFor

<div *ngFor="#hero of heroes">{{hero.fullName}}</div>

<!-- NgFor with index -->
<div *ngFor="#hero of heroes, #i=index">{{i + 1}} - {{hero.fullName}}</div>

 

NgForm

<form (ngSubmit)="onSubmit(theForm)" #theForm="ngForm">
  <div class="form-group">
    <label for="name">Name</label>
    <input class="form-control" required ngControl="firstName"
      [(ngModel)]="currentHero.firstName">
  </div>
  <button type="submit" [disabled]="!theForm.form.valid">Submit</button>
</form>

 

输入输出值

输入属性通常接收数据值。输出属性实现事件生产者

@Input()  hero: Hero;
@Output() deleted = new EventEmitter<Hero>();

 

Anauglar2中的模板语法

标签:

原文地址:http://www.cnblogs.com/bq-med/p/5310905.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!