[英]AngularJS Factory Info inconsistent
我有一個包含對象數組的工廠。 每個對象都包含有關員工的信息,如下所示:
DataFactory :
app.factory('DataFactory', function(){
return [
{
'name': 'John Smith',
'shifts':[ //this might need to be {}
{'start':new Date(2014, 06, 25, 1, 30),
'end': new Date(2014, 06, 25, 22, 30)
},
{'start':'/*somedate*/',
'end': '/*someDate*/'
}
]
}
];
});
我試圖根據使用該應用程序的用戶選擇的用戶在一個名為CurrentUser的工廠中設置一個值。
CurrentUser :
app.factory('CurrentUser', ['DataFactory', function(DataFactory){
var service = {};
service.indexVal;
service.user = DataFactory[service.indexVal];
return service;
}]);
HTML :
<select ng-model="CurrentUser.indexVal">
<option ng-repeat="employee in DataFactory" value="{{$index}}">
{{employee.name}}
</option>
</select>
Current User: {{CurrentUser.user.name}}<br />
但是,這樣就沒有名稱值。 選擇名稱后,它不會顯示“ john smith”。
當我將其保留為CurrentUser.user而不是顯示用戶的整個對象時,它將顯示indexVal。
如何顯示名稱?
如果要綁定到模型值,請使用ng-options:
<select ng-model="CurrentUser.user" ng-options="employee as employee.name for employee in DataFactory">
</select>
Current User: {{CurrentUser.user.name}}<br />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.