1

我有一个包含对象数组的工厂。每个对象都包含有关员工的信息,如下所示:

数据工厂

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 的工厂中设置一个值。

当前用户

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。

如何让它显示名称?

4

1 回答 1

1

如果要绑定到模型值,请使用 ng-options:

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

Current User: {{CurrentUser.user.name}}<br />
于 2014-07-26T21:27:08.847 回答