angular2多种绑定

作者: GTReload | 来源:发表于2016-12-07 16:17 被阅读0次

angular提供绑定机制,解决mode-view耦合。

先看下Demo运行效果

1111.gif

data单向绑定

app.component.ts

import { Component } from '@angular/core';

@Component({
    moduleId: module.id,
    selector: 'my-app',
    templateUrl: 'app.component.html'
})

export class AppComponent {
    name: string = "static";
}

app.component.html

<h2>{{name}}</h2>

data双向绑定

在app.module.ts导入FormsModule

import { FormsModule } from '@angular/forms';

@NgModule({
    imports: [BrowserModule,FormsModule],
    ...

修改app.component.html

<h2>{{name}}</h2>
name:<input [(ngModel)]="name" placeholder="name" />

事件绑定

app.component.html尾部新增

<button (click)="changeTitle()">change</button>

app.component.ts中实现changeTitle()方法

changeTitle(): void {
    this.name="sider";
}

style简单绑定

app.component.html

<h2 [style.color]="isStatic() ? 'red':'blue'">{{name}}</h2>

app.component.ts

isStatic(): Boolean {
    return this.name === 'static';
}

style组合绑定

app.component.html

<!--<h2 [style.color]="isStatic() ? 'red':'blue'">{{name}}</h2>-->
# <h2 [ngStyle]="setStyles()">{{name}}</h2>

app.component.ts

    setStyles(): Object {
        let styles = {
            'color': this.isStatic() ? 'red' : 'blue',
            'font-size': this.isStatic() ? '3rem' : '2rem',
            'font-style': this.isStatic() ? 'italic' : 'normal'
        };
        return styles;
    }

class绑定

新增app.component.css文件且在app.component.ts引用

@Component({
    moduleId: module.id,
    selector: 'my-app',
    templateUrl: 'app.component.html',
    styleUrls: ['app.component.css']
})
  • class

app.component.css

.awestatic {
    color: red;
}

app.component.html

<h2 [class.awestatic]="isStatic()">{{name}}</h2>
  • ngClass

app.component.html

<!--<h2 [style.color]="isStatic() ? 'red':'blue'">{{name}}</h2>-->
<!--<h2 [ngStyle]="setStyles()">{{name}}</h2>-->
<!--<h2 [class.awestatic]="isStatic()" [class.awesider]="!isStatic()">{{name}}</h2>-->
<h2 [ngClass]="setClasses()">{{name}}</h2>

app.component.css

.awestatic {
    color: red;
    font-style: italic;
}

.awesider {
    color: blue;
    font-style: normal;
}

.move {
    position: relative;
    -webkit-animation: move 1s infinite;
    -webkit-animation-duration: 3s;
}

@-webkit-keyframes move {
    0% {
        left: 0px;
    }
    50% {
        left: 200px;
    }
    100% {
        left: 0px;
    }
}

app.component.ts

    setClasses(): Object {
        let classes = {
            awestatic: this.isStatic(),
            awesider: !this.isStatic(),
            move: !this.isStatic()
        };
        return classes;
    }

相关文章

  • angular2多种绑定

    angular提供绑定机制,解决mode-view耦合。 先看下Demo运行效果 data单向绑定 app.com...

  • angular2--数据绑定

    今天要跟大家分享的是angular2模板中涉及到的数据绑定,Angular提供了多种数据绑定方式,可以根据数据流...

  • angular2:数据绑定的基本概念

    前面有写到input的双向数据绑定,但除了双向数据绑定外,还存在单向数据绑定的概念,比如angular2:组件间的...

  • ionic4 Hybrid App开发(二)

    一、angular2基本语法 ** a、数据绑定** ** b、ng指令** 需要注意的是,...

  • angular2 ---- 绑定

    数据源到视图目标的绑定 即从组件中读取数据展示在视图上,如,使用字符串设置元素的属性值。 这里的属性值(DOM的p...

  • 走进Angular4的大门

    版本发行速度 Angular2 Angular新架构 javascript不可变对象 组件类 数据绑定 依赖注入 ...

  • [Guice] 5 绑定

    绑定的方式有很多种:<1> 类名绑定<2> 实例绑定<3> 连接绑定<4> Provider绑定<5> 命名绑定<...

  • Angular2 核心概念

    @[toc] Angular2的核心组件 组件 元数据 模板 数据绑定 服务 指令 依赖注入 模块在这里插入图片描...

  • Angular2的数据绑定

    前言 NG的核心部分之一就是数据绑定。它们是内嵌在HTML里面的表达式,用来在HTML文档里产生动态内容。 举个例...

  • 事件绑定的多种方式

    正在看第二遍锋利的jQuery,第一遍因为只是在草稿纸上手写代码所以看完之后印象不是很深,这次决定把书上的案例都在...

网友评论

    本文标题:angular2多种绑定

    本文链接:https://www.haomeiwen.com/subject/moadmttx.html