gpt4 book ai didi

javascript - 异步 ngOptions

转载 作者:行者123 更新时间:2023-11-30 06:53:04 24 4
gpt4 key购买 nike

老实说,我不知道我在 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>

这里是 Controller (模板的 Controller 是来自$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...我几乎没有想法。我根本无法在选择中显示任何数据。任何帮助将不胜感激!谢谢。

最佳答案

我认为这应该可行:

userService.getUser().then(function(user) {
$scope.advertisers = user.getAdvertisers();
});

或者如果 getAdvertisers 返回一个 promise :

userService.getUser().then(function(user) {
user.getAdvertisers().then(function (advertisers) {
$scope.advertisers = advertisers;
});
});

关于javascript - 异步 ngOptions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29134596/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com