2015-10-19 36 views
0

假設我有以下的數據,我使用角事先鍵入的內容:轉換價值在飛行中

$scope.users= UserResource.query(); 
//$scope.users= [{id: "1", name: "John"}, {id:"2", name: "Tom"}] 

是否有可能作出預輸入綁定到號ID,而不是字符串ID服務器獲取號碼? 例如,我希望$ scope.person.id爲而不是「1」因此,當我選擇一個值時,我不會收到任何投訴。現在,因爲ID是字符串類型的,我的號碼字段得到這個錯誤:錯誤:[ngModel:numfmt]預計1是一些

事情是這樣的:

<input ng-model="person.id class="form-control" type="number" typeahead="parseInt(user.id) as user.name for user in users | filter:$viewValue" /> 

以上沒有按不起作用,下列情況也是如此:

<input ng-model="person.id class="form-control" type="number" typeahead="user.id as user.name for user in users | filter: number | filter:$viewValue" /> 

PS當然,我不需要對id字段進行任何客戶端驗證。這只是一個快30秒的例子來說明問題。

+1

將取代'型= number'用'類型=文本模式=「\ d +」'是一種選擇「 – Icycool

+0

是的,這是?一個很好的選擇,我從來沒有意識到!如果你可以發佈這個答案,會很棒。 –

回答

0

你可以寫你自己的過濾器,這和使用它:

user.id as user.name for user in (users | stringToId:'user.id') 
相關問題