日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Angular应用双向绑定的语法糖

發(fā)布時(shí)間:2023/12/19 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Angular应用双向绑定的语法糖 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Angular[()]的語法暗示了element具有一個(gè)可以賦值的名為x的property,以及一個(gè)對(duì)應(yīng)的xChange事件。

一個(gè)例子:

import { Component, Input, Output, EventEmitter } from '@angular/core';@Component({selector: 'app-sizer',templateUrl: './sizer.component.html',styleUrls: ['./sizer.component.css'] }) export class SizerComponent {@Input() size: number | string;@Output() sizeChange = new EventEmitter<number>();dec() { this.resize(-1); }inc() { this.resize(+1); }resize(delta: number) {this.size = Math.min(40, Math.max(8, +this.size + delta));this.sizeChange.emit(this.size);}}

size Component的size屬性加上了@Input注解,使得parent Component在顯示它的時(shí)候,可以進(jìn)行property binding. sizeChange通過@Output修飾,這樣可以給external receiver發(fā)送event.

parent Component的fontSizePx同child Component的size property進(jìn)行雙向綁定。

<app-sizer [(size)]="fontSizePx"></app-sizer> <div [style.font-size.px]="fontSizePx">Resizable Text</div>

AppComponent的fontSizePx通過雙向綁定的方式關(guān)聯(lián)到了SizerComponent.

這個(gè)[(size)]的語法只是一個(gè)語法糖,實(shí)際會(huì)被轉(zhuǎn)換成如下代碼:

<app-sizer [size]="fontSizePx" (sizeChange)="fontSizePx=$event"></app-sizer>

The $event variable contains the payload of the SizerComponent.sizeChange event. Angular assigns the $event value to the AppComponent.fontSizePx when the user clicks the buttons.

總結(jié)

以上是生活随笔為你收集整理的Angular应用双向绑定的语法糖的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。