<ul>
<li *ngFor="let race of races">
{{race.name}}
</li>
</ul>
//定义模板字符串
var source=`
<ul>
{{#each races}}
<li>{{name}}</li>
{{/each}}
</ul>
`;
//在运行时把模板字符串编译成JS函数
var templateFn=Handlebars.compile(source);
//把数据传给模板函数,获得最终的HTML
var html=templateFn([
{name:'人族'},
{name:'神族'},
{name:'虫族'}
]);
它可以获取到组件里面定义的属性值。请依次看例子:
它可以自动计算简单的数学表达式,例如:加减乘除、取模。
它可以获得方法的返回值。
private gameName:String = "王者荣耀";
<div>
{{gameName}}
</div>
<h3>1+1={{1+1}}</h3>
<h3>可以调用方法{{getVal()}}</h3>
public getVal():any{
return 65535;
}
<input type="text" #heroInput>
<p>{{heroInput.value}}</p>
<p>{{heroInput.text}}</p>
<img [src]="imgSrc" />
public imgSrc:string="./assets/imgs/1.jpg";
<button class="btn btn-success" (click)="btnClick($event)">测试事件</button>
public btnClick(event):void{
alert("测试事件绑定!");
}
<input type="text" [(ngModel)] = "dobuleBindName"/>
{{dobuleBindName}}
private dobuleBindName:String = "";
<div *ngIf="showed" >显示/不显示</div>
<button type="button" (click) = "toggleShow()">切换</button>
private showed:boolean = false;
public toggleShow():void{
this.showed = !this.showed;
}
<div *ngFor="let fruit of fruits;let i = index; ">
<div>{{i+1}}-{{fruit}}</div>
</div>
private fruits:Array<any> = ['苹果', '香蕉', '樱桃'] ;
<div [ngSwitch]="mapStatus">
<p *ngSwitchCase="0">下载中...</p>
<p *ngSwitchCase="1">正在读取...</p>
<p *ngSwitchDefault>系统繁忙...</p>
</div>
public mapStatus:number=1;
<div [ngClass]="currentClasses">同时批量设置多个样式</div>
<button class="btn btn-success" (click)="setCurrentClasses()">设置</button>
public currentClasses: {};
public canSave: boolean = true;
public isUnchanged: boolean = true;
public isSpecial: boolean = true;
setCurrentClasses() {
this.currentClasses = {
'saveable': this.canSave,
'modified': this.isUnchanged,
'special': this.isSpecial
};
}
.saveable{
font-size: 18px;
}
.modified {
font-weight: bold;
}
.special{
background-color: #ff3300;
}
<div [ngStyle]="currentStyles">
用NgStyle批量修改内联样式!
</div>
<button class="btn btn-success" (click)="setCurrentStyles()">设置</button>
public currentStyles: {}
public canSave:boolean=false;
public isUnchanged:boolean=false;
public isSpecial:boolean=false;
setCurrentStyles() {
this.currentStyles = {
'font-style': this.canSave ? 'italic' : 'normal',
'font-weight': !this.isUnchanged ? 'bold' : 'normal',
'font-size': this.isSpecial ? '36px' : '12px'
};
}
<p class="text-danger">ngModel只能用在表单类的元素上面</p>
<input [(ngModel)]="currentRace.name">
<p>{{currentRace.name}}</p>
public currentRace:any={name:"随机种族"};
{{currentTime | date:'yyyy-MM-dd HH:mm:ss'}}
public currentTime: Date = new Date();
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |