问题描述
如何在Angular中为multiselect选择标记设置多个默认值? 这是我的标签:
<select multiple="multiple"
class="form-control multiselect"
ng-model="dog.Tags"
ng-options="tag as tag.Name for tag in tagsAvailable">
</select>
我在我的控制器中调用的服务中有一个Ajax调用,该服务返回Dog对象。 Dog对象看起来像:
{ID: 1, Name: "Lassie", Tags: [{TagID: 2, Name: "Red"},{TagID: 3, Name: "Barker"}]}
这是控制器中的方法,该方法调用返回Dog JSON对象的服务。
dogSvc.getDog($routeParams.dogId).then(
function (response) {
$scope.dog = response.data;
},
function (response) {
$scope.status = response.status;
});
我想将默认的选定值设置为Dog.Tags值。
然后,我有一个类似的服务,该服务返回所有可用的标签,我希望将这些标签填充为选择框中的选项。
tagSvc.getAllTags().then(
function (response) {
$scope.tagsAvailable = response.data;
},
function (response) {
$scope.status = response.status;
});
填充可用标签,但不填充默认值。 如何根据返回的Dog对象的标签预先选择选项?
1楼
Matt
0
已采纳
2015-07-30 20:37:58
这就是我最终要做的
$q.all([tagSvc.getAllTags(), dogSvc.getDog($routeParams.dogId)]).then(
function (responses) {
$scope.tagsAvailable = responses[0].data;
$scope.dog = responses[1].data;
$scope.dog.Tags = $scope.tagsAvailable.filter(
function (tag) {
return $scope.dog.Tags.some(
function (elem) {
if (elem.TagID == tag.TagID) return true;
});
});
});
基本上是这样的:
- 检索可用标签。
- 检索现有标签,它将成为默认标签。
- 获取可用标签并对其进行过滤,以便仅包括现有标签阵列中存在的可用标签。
- 将过滤后的可用标签设置为模型的标签值。