(十)、Angular4.0 数据绑定、管道

一、基本HTML属性绑定

  • tableColspan的值会赋给colspan
<td [attr.colspan]="tableColspan">Something</td>

二、CSS类绑定

  • [class]中的值someExpression会覆盖"aaa bbb"
<div class="aaa bbb" [class]="someExpression">something</div>

  • 如果isSpecial为true,则显示[class.special]中的special的值
<div [class.special]="isSpecial">something</div>
  • 控制多个class是否显示
<div [ngClass]="{aaa:isA, bbb:isB}"></div>


三、样式绑定

  • 如果isSpecial为true,则color为red
<button [style.color]="isSpecial ? 'red':'green'"></button>

  • font-style是要覆盖的样式 , 如果canSave为false,则使用normal
<button [ngStyle]="{'font-style':this.canSave ? 'itailc':'normal'}"></button>

四、双向绑定 (每3秒会重置name为Tom)

app.component.ts

  name: string;

  constructor() {
    setInterval(() => {
      this.name = 'Tom';
    }, 3000);
  }

app.component.html

<input [(ngModel)]="name">
{{name}}

五、新建自定义管道

ng g pipe pipe/multiPle
注意查看@NgModule中的declarations属性有没有MultiPlePipe

六、定义自己的管道逻辑 multi-ple.pipe.ts

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'multiPle'
})
export class MultiPlePipe implements PipeTransform {

  transform(value: any, args?: any): any {
    if (!args) {
      args = 1;
    }
    return value * args;
  }

}

七、app.component.html中 使用管道

<p>测试:{{size | multiPle:'2'}}</p>

八、app.component.ts 中给size赋值

  size = 7;

九、启动项目,测试结果:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值