老实说,我不知道我在 Angular 中在做什么,所以这可能是糟糕的编码实践。无论如何,我正在尝试将一些数据<select>
异步加载到元素中。
这是模板 HTML:
<div ng-include src="'scripts/common/toolbar/toolbar.html'"></div>
<div class="wrapper">
<div ng-include src="'scripts/common/header/pageheader.html'"></div>
<label>Advertisers:
<select ng-model="selectedAdvertiser" ng-options="adv as adv.name for adv in advertisers"></select>
</label>
</div>
这是控制器(模板的控制器是来自 $routeProvider 的 HomeCtrl)
angular.module('feedsmanagerApp').controller('HomeCtrl', function ($scope, $rootScope, $location, ngDialog, userService) {
$scope.advertisers = [];
userService.getUser().then(function(user) {
$scope.$evalAsync(function() {
$scope.advertisers = user.getAdvertisers();
})
});
//more stuff down here....
从 user.getAdvertisers() 返回的数据如下所示:
[{name:'aaaa', id:0}, {name:'bbbb', id:1}];
最后,这里是 userService
var userPromise,
getUser = function() {
if(!userPromise) {
var deferred = $q.defer();
userPromise = deferred.promise;
deferred.resolve(User.createFromServerData(null));
}
return userPromise;
};
我试过使用 $timeout、$scope.$apply、$scope.$evalAsync ......我几乎没有想法。我根本无法在选择中显示任何数据。任何帮助将不胜感激!谢谢。