2014-09-26 49 views
0

我有一個HTML模板,我想在用戶創建一個項目時以及在編輯時使用。ng-model取決於控制器

我想要做這樣的事情:

<input ng-model="newItem.description ? newItem.description : item.description"></input> 

我知道它可以用這個來完成:

<input ng-if="newItem.description" ng-model="newItem.description"></input> 
<input ng-if="item.description" ng-model="item.description"></input> 

但是,有沒有辦法做到這一點只用一條線?

謝謝!

+0

爲什麼不簡單''? – Grundy 2014-09-26 19:33:42

+0

@Grundy,因爲我想保持單獨的newItem和項目 – 2014-09-26 19:34:37

+0

請注意,如果我問什麼是上下文?這是否像創建臨時更改並保存它們之前保存它們? – kevin628 2014-09-26 19:35:32

回答

4
AngularJS docs

ngModel會嘗試綁定到通過對當前範圍計算表達式給出的屬性

所以你可以使用表達式爲獲取屬性你需要什麼,這樣的事情

<input ng-model="(newItem || item).description" /> 
相關問題