第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

在自定義屬性指令和數(shù)據(jù)綁定屬性指令中使用 @Attribute

在自定義屬性指令和數(shù)據(jù)綁定屬性指令中使用 @Attribute

紫衣仙女 2021-12-02 10:36:43
我是 Angular 的新手,只是一個(gè)關(guān)于在屬性指令中使用 @Attribute 的問題,下面是書中的一些代碼:@Directive({ selector: "[pa-attr]",})export class PaAttrDirective {    constructor(element: ElementRef, @Attribute("pa-attr") bgClass: string) {       element.nativeElement.classList.add(bgClass || "bg-success", "text-white"); }}和模板.html:...<td pa-attr="bg-warning">{{item.category}}</td>...所以我們可以看到使用@Attribute我們可以獲得屬性的值,但是如果我們使用數(shù)據(jù)綁定屬性指令為:<td [pa-attr]="item.category == 'Soccer' ? 'bg-info' : null">...然后書修改代碼為:export class PaAttrDirective {   constructor(private element: ElementRef) {}   @Input("pa-attr")   bgClass: string;   ngOnInit() {      this.element.nativeElement.classList.add(this.bgClass || "bg-success", "text-white");   }}我在這里有點(diǎn)困惑,我們不能使用 @Attribute 再次獲取值:export class PaAttrDirective {    constructor(element: ElementRef, @Attribute("pa-attr") bgClass: string) {       element.nativeElement.classList.add(bgClass || "bg-success", "text-white"); }}為什么當(dāng)將屬性指令與數(shù)據(jù)綁定一起使用時(shí),我們必須在代碼中創(chuàng)建輸入屬性而不能使用@Attribute?
查看完整描述

2 回答

?
互換的青春

TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超6個(gè)贊

他們使用@Input而不是@Attribute因?yàn)椋?/p>

屬性初始化 DOM 屬性,然后它們就完成了。屬性值可以改變;屬性值不能。

item.category == 'Soccer' ? 'bg-info' : null 表達(dá)式更改屬性值,因此您的指令在更改后不會獲得更新的值。

我建議在這里閱讀Angular 模板語法。


查看完整回答
反對 回復(fù) 2021-12-02
?
牛魔王的故事

TA貢獻(xiàn)1830條經(jīng)驗(yàn) 獲得超3個(gè)贊

接受簡單的原始類型,例如字符串和數(shù)字 @Input:接受任何東西/一個(gè)對象,例如您自己的類對象

abc可以像這樣將字符串傳遞給屬性:

<td pa-attr="abc"></td>

您將相同的內(nèi)容傳遞給輸入,如下所示:

<td [pa-attr]="'abc'"></td> <!-- note the single quotes -->

或者在 ts

x = 'abc';

在 html 中

<td [pa-attr]="x"></td>

我不確定您是否可以在輸入屬性名稱中使用破折號。


查看完整回答
反對 回復(fù) 2021-12-02
  • 2 回答
  • 0 關(guān)注
  • 255 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號