2014-07-26 33 views
1

我有一個包含對象數組的工廠。每個對象都包含關於僱員的信息,像這樣:AngularJS工廠信息不一致

的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 /> 

然而,與此,沒有名稱值。選擇名稱後不會顯示「約翰史密斯」。

當我將它作爲CurrentUser.user而不是顯示用戶的整個對象時,它會顯示indexVal。

如何讓它顯示名稱?

回答

1

使用NG選項,如果你希望綁定到一個模型值:

<select ng-model="CurrentUser.user" ng-options="employee as employee.name for employee in DataFactory"> 
</select> 

Current User: {{CurrentUser.user.name}}<br />