使用多个选项以角度md-select设置所选项目 [英] Setting selected item in angular md-select with multiple option

查看:193
本文介绍了使用多个选项以角度md-select设置所选项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用多个选项以有角度的md-select设置所选项目

Setting selected item in angular md-select with multiple option

<md-select multiple ng-model="Reg.roles" placeholder="Please select roles" required>
    <md-option ng-repeat="role in roles" value="{{role.value}}" ng-selected="{{ role.value === '<%= data.roles %>' ? 'true' : 'false' }}">{{ role.name }}</md-option>
</md-select>

在我的控制器中

in my controller

 $scope.roles = [{
        "name": "Account Admin",
        "value": "account_admin"
    }, {
        "name": "Developer",
        "value": "developer"
    },


    {
        "name": "Analyst",
        "value": "analyst"
    }
];

可见

data.roles 包含值:

['account_admin', 'developer']

我需要与 role.value 相对应的项目处于选中状态.

I need the item corresponding to role.value should be in selected state.

请参考下图

推荐答案

最后,我找到了解决方案,在我的控制器中,我这样写道:

Finally i found the solution, in my controller i wrote this:

var current_roles       = '<%=data.roles%>'; // current value from db as array

$scope.Reg.roles = []; //initialize array

angular.forEach($scope.roles, function(val1, key1) {
    if(current_roles.indexOf(val1['value']) !== -1) // check if item in current array
    {
        $scope.Reg.roles.push(val1['value']); //setting to select items
    }
});

这篇关于使用多个选项以角度md-select设置所选项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆