gpt4 book ai didi

javascript - 使用 $http 在 angular-bootstrap-duallistbox 中设置所选项目

转载 作者:行者123 更新时间:2023-12-03 08:53:32 25 4
gpt4 key购买 nike

使用 angular-bootstrap-duallistbox 组件,记录为 here ,并使用 $http 服务,如何在其上设置所选元素?

这里是使用的代码:

HTML:

<select ng-options="obj as obj.name for obj in authorizations"
multiple
ng-model="selections"
bs-duallistbox
></select>

Javascript:

angular.module('demoApp', ['frapontillo.bootstrap-duallistbox'])
.controller('ProfileAutAsigController',
function ($scope, $http, Authorization) {

$scope.authorizations = [];
$scope.selections = [];

Authorization.query().$promise.then(function (response) {
$scope.authorizations = response;
return $http.get("api/profileAut/1/authorizations");

}).then(function (payload) {
//This doesn't set the selected items. Specifying an
//array manually doesn't work either.
//$scope.selections = [{id:1, name:'Text'},{id:3, name:'Photos'}];

$scope.selections = payload.data;

}, function (payload) {
//Error happened
console.log(payload.error);
});
});

谢谢。

最佳答案

要填充 Angular 引导双列表框,您应该使用 track by 而不是 as,如下所示:

您必须在 ng-options 中使用 track by 而不是 as

而是这样:

 <select ng-model="selected" ng-options="av as av.name for av in available" multiple bs-duallistbox></select>

用途:

 <select ng-model="selected" ng-options="av.name for av in available track by av.id" multiple bs-duallistbox></select>

关于javascript - 使用 $http 在 angular-bootstrap-duallistbox 中设置所选项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32597256/

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