2016-06-08 68 views
0

我在我的html頁面上有自定義指令。對於我的組件msgfooter我有不同的錯誤類型的消息:在Angular 2上綁定自定義指令

<msgfooter [errorType]="Invalid server"> <msgfooter> 

<msgfooter [errorType]="Few Parameters"> <msgfooter> 

我通常創建我的.ts文件中的字符串。但在自定義指令我不能做到這一點:

<msgfooter [errorType]={{myCustomMessage}}> <msgfooter> 

錯誤:

Parser Error: Got interpolation ({{}}) where expression was expected at column 0 

我怎樣才能解決這個問題?

回答

2

在角2,你能夠做3種輸入:

<msgfooter [errorType]="myCustomMessage"><msgfooter> 

<msgfooter errorType="Invalid server"><msgfooter> 

<msgfooter errorType="{{myCustomMessage}}"> 

第一個將進行評價,它會在組件中查找特定變量(myCustomMessage)。

第二個將只傳遞一個字符串。

第三個將評估變量myCustomMessage,對其進行字符串化並將其傳遞給errorType輸入。


只能使用[]{{}}但不能兩者都在同一時間。

+0

實際上,第三個字符串仍然是字符串,它不會評估變量。你基本上只是傳遞一個'{{myCustomMessage}}'字符串值。 第三個輸入將是'bind-errorType =「myCustomMessage」'。 –

+0

@EdMorales不是。當傳遞給屬性時,Angular 2會評估「{{}}」。嘗試玩它。 –

+0

剛剛做了,沒有工作。 –