gpt4 book ai didi

javascript - Angular 选择延迟加载项目的默认值

转载 作者:行者123 更新时间:2023-12-03 10:40:20 24 4
gpt4 key购买 nike

所以我有以下 Controller :

    app.controller('ProfileController', ['$http','$sessionStorage','api', '$scope','Session', function ($http,$sessionStorage, api, $scope, $state, Session) {

$scope.titles = {};
$scope.selected_title = $sessionStorage.activeUser.user.title.id;

$http.get(api.getUrl('title',null))
.success(function(response) {
$scope.titles = response;
});

}]);

现在我有以下选择:

<select ng-init="selected_title.id" class="form-control" ng-model="selectedItem" ng-options="item as item.name for item in titles"></select>

但是,由于 select 的选项是使用 http 加载的,因此它将在页面加载后加载,因此默认值为空。

谁能告诉我如何避免这种情况?

最佳答案

ngInit 不会那样工作,它不是默认值。

    app.controller('ProfileController', ['$http','$sessionStorage','api', '$scope','Session', function ($http,$sessionStorage, api, $scope, $state, Session) {

$scope.titles = [];

$http.get(api.getUrl('title',null))
.success(function(response) {
$scope.titles = response;
for (var i = 0; i < response.length; i++) {
if (response[i].id == $sessionStorage.activeUser.user.title.id) {
$scope.selectedItem = response[i];
break;
}
}
});

}]);

并从您的选择中删除 ngInit

Documentation for ngInit.

关于javascript - Angular 选择延迟加载项目的默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28811029/

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