2016-12-29 148 views
1

當我添加一個新的動態文本字段時,我將它添加爲「0」,默認情況下,我想做一些事情,如果值爲「0」,則用「」替換它。如何在ng模型中創建三元運算符? Angular.js

 $scope.add=function(){ 
      $scope.aAnimals.push({ "animal": "0"}) 
    } 

    <div ng-repeat='item in aAnimals'> 
    THIS IS MY IDEA in ng-model but i have an error..<!--item.animal=='0'?'':item.animal--> 
    <input type='text' ng-model=item.animal class='animal' /> 
    </div> 

http://plnkr.co/edit/ukTl0ho5gPpOE6jMLq11?p=preview

+0

你不應該這樣做。你能告訴你想要達到什麼嗎?你可以採取其他方式。 –

+0

可能很明顯,但爲什麼不''scope.aAnimals.push({「animal」:「」})' – phuzi

+0

@phuzi默認情況下不能有「animal」:「」,這會產生下拉問題 – yavg

回答

1

爲創建過濾器:

app.filter('blankZero', function() { 
    return function (text) { 

    if(text=="0") 
     return ""; 

    return text; 
    }; 
}); 

使用它在視圖:

{{item.animal|blankZero}} 
+0

where use {{item.animal | blankZero}}? – yavg

+0

@yavg模板內ng-repeat –

+0

但不適用於我..我需要這個ng模型 – yavg