2016-08-03 39 views
11

我想一個指令有條件地添加到組件(其中au-disabledau-accented,並au-focused是指令)添加指令:如何有條件地在角2

<template [ngIf]="disabled"> 
    <au-placeholder au-disabled></au-placeholder> 
</template> 

<template [ngIf]="accented"> 
    <au-placeholder au-accented></au-placeholder> 
</template> 

<template [ngIf]="focused"> 
    <au-placeholder au-focused></au-placeholder> 
</template> 

上述方法的工作原理(是有點接受對我來說),因爲(就我而言)條件屬性disabledaccentedfocused是相互排斥的 - 我的問題出現在條件屬性不互相排斥的情況下(每個排列要求應用[ngIf]來應用相應的變形表單):

<!-- all of the prior <template [ngIf]= ... --> 

<!-- plus --> 

<template [ngIf]="disabled && accented"> 
    <au-placeholder au-disabled au-accented></au-placeholder> 
</template> 

<template [ngIf]="disabled && accented && focused"> 
    <au-placeholder au-disabled au-accented au-focused></au-placeholder> 
</template> 

<!-- etc --> 

使用下面允許我的代碼能夠以較少的HTML處理組合:

<au-placeholder [au-disabled]="disabled" [au-accented]="accented" [au-focused]="focused"></au-placeholder> 

,但呈現的HTML始終都各持真值的指令...該組件必須測試每個指令的真實價值都會得到適當的迴應,但是即使不適用不相關的指令也會更清晰。有一個更好的方法嗎?

回答

3

這不支持。 只能有條件地添加/移除組件。

你可以做的是傳遞一個值,使指令意識到它不應該做任何事情。

又見https://github.com/angular/angular/issues/5332

+1

OK ......這是什麼'<太子港佔位符[AU-禁用= 「禁用」[AU口音] = 「重音符號」[AU-關注] = 「強調」>'表格正在做... – Neoheurist