angular2多種綁定

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導(dǎo)入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中實現(xiàn)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;
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末傻丝,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌蒂萎,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,858評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件淮椰,死亡現(xiàn)場離奇詭異五慈,居然都是意外死亡,警方通過查閱死者的電腦和手機主穗,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,372評論 3 395
  • 文/潘曉璐 我一進店門泻拦,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人忽媒,你說我怎么就攤上這事争拐。” “怎么了晦雨?”我有些...
    開封第一講書人閱讀 165,282評論 0 356
  • 文/不壞的土叔 我叫張陵架曹,是天一觀的道長。 經(jīng)常有香客問我闹瞧,道長绑雄,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,842評論 1 295
  • 正文 為了忘掉前任奥邮,我火速辦了婚禮万牺,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘漠烧。我一直安慰自己杏愤,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,857評論 6 392
  • 文/花漫 我一把揭開白布已脓。 她就那樣靜靜地躺著珊楼,像睡著了一般。 火紅的嫁衣襯著肌膚如雪度液。 梳的紋絲不亂的頭發(fā)上厕宗,一...
    開封第一講書人閱讀 51,679評論 1 305
  • 那天,我揣著相機與錄音堕担,去河邊找鬼已慢。 笑死,一個胖子當(dāng)著我的面吹牛霹购,可吹牛的內(nèi)容都是我干的佑惠。 我是一名探鬼主播,決...
    沈念sama閱讀 40,406評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼膜楷!你這毒婦竟也來了旭咽?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,311評論 0 276
  • 序言:老撾萬榮一對情侶失蹤赌厅,失蹤者是張志新(化名)和其女友劉穎穷绵,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體特愿,經(jīng)...
    沈念sama閱讀 45,767評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡仲墨,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了揍障。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片目养。...
    茶點故事閱讀 40,090評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖亚兄,靈堂內(nèi)的尸體忽然破棺而出混稽,到底是詐尸還是另有隱情,我是刑警寧澤审胚,帶...
    沈念sama閱讀 35,785評論 5 346
  • 正文 年R本政府宣布匈勋,位于F島的核電站,受9級特大地震影響膳叨,放射性物質(zhì)發(fā)生泄漏洽洁。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,420評論 3 331
  • 文/蒙蒙 一菲嘴、第九天 我趴在偏房一處隱蔽的房頂上張望饿自。 院中可真熱鬧,春花似錦龄坪、人聲如沸昭雌。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,988評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽烛卧。三九已至,卻和暖如春妓局,著一層夾襖步出監(jiān)牢的瞬間总放,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,101評論 1 271
  • 我被黑心中介騙來泰國打工好爬, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留局雄,地道東北人。 一個月前我還...
    沈念sama閱讀 48,298評論 3 372
  • 正文 我出身青樓存炮,卻偏偏與公主長得像炬搭,于是被迫代替她去往敵國和親蜈漓。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,033評論 2 355

推薦閱讀更多精彩內(nèi)容