Angularjs AngularNG选定的问题

Angularjs AngularNG选定的问题,angularjs,Angularjs,我有一个多选的。我有两个不同的数组。我想选择带有选定值的多选 我想从数组中搜索ng repeat id,如果找到,则它将变为true,并且值将随选定项一起出现 我的AngularJS代码 <script> var app = angular.module('myApp', []); app.controller('MainCtrl', function($scope, $http) { $scope.name = "John Brad";

我有一个多选的。我有两个不同的数组。我想选择带有选定值的多选

我想从数组中搜索
ng repeat id
,如果找到,则它将变为true,并且值将随选定项一起出现

我的AngularJS代码

<script>
      var app = angular.module('myApp', []);
    app.controller('MainCtrl', function($scope, $http) {

      $scope.name = "John Brad";

      $scope.important_selection = {
                  "error": false,
                  "client_data": {
                  "important": [
                      {
                          "id": 10,
                          "name": "Socially Responsible"
                      },
                      {
                          "id": 8,
                          "name": "LBGT"
                      },
                      {
                          "id": 4,
                          "name": "Education"
                      },
                      {
                          "id": 2,
                          "name": "Retirement"
                      }
                  ]
                },
                "status_code": 200
              };

    $scope.importants_series = {
                "error": false,
                "important": [
                    {
                        "id": 1,
                        "name": "Investment"
                    },
                    {
                        "id": 2,
                        "name": "Retirement"
                    },
                    {
                        "id": 3,
                        "name": "Insurance"
                    },
                    {
                        "id": 4,
                        "name": "Education"
                    },
                    {
                        "id": 5,
                        "name": "Tax"
                    },
                    {
                        "id": 6,
                        "name": "Estate"
                    },
                    {
                        "id": 7,
                        "name": "Business"
                    },
                    {
                        "id": 8,
                        "name": "LBGT"
                    },
                    {
                        "id": 9,
                        "name": "Offshore"
                    },
                    {
                        "id": 10,
                        "name": "Socially Responsible"
                    },
                    {
                        "id": 11,
                        "name": "Divorce"
                    }
                ],
                "status_code": 200
            };          
    });
    </script>
<select name="important[]" class="form-control" multiple="" required>
        <option ng-selected="important_selection.important.find(item => item.id === important.id)" ng-repeat="important in importants_series.important" value="{{important.id}}">{{important.name}}</option>
    </select>

var-app=angular.module('myApp',[]);
app.controller('MainCtrl',函数($scope,$http){
$scope.name=“John Brad”;
$scope.important\u选择={
“错误”:错误,
“客户数据”:{
“重要”:[
{
“id”:10,
“姓名”:“对社会负责”
},
{
“id”:8,
“名称”:“LBGT”
},
{
“id”:4,
“姓名”:“教育”
},
{
“id”:2,
“姓名”:“退休”
}
]
},
“状态代码”:200
};
$scope.importants\u系列={
“错误”:错误,
“重要”:[
{
“id”:1,
“名称”:“投资”
},
{
“id”:2,
“姓名”:“退休”
},
{
“id”:3,
“名称”:“保险”
},
{
“id”:4,
“姓名”:“教育”
},
{
“id”:5,
“名称”:“税”
},
{
“id”:6,
“姓名”:“遗产”
},
{
“id”:7,
“名称”:“业务”
},
{
“id”:8,
“名称”:“LBGT”
},
{
“id”:9,
“名称”:“离岸”
},
{
“id”:10,
“姓名”:“对社会负责”
},
{
“id”:11,
“姓名”:“离婚”
}
],
“状态代码”:200
};          
});
我的HTML代码

<script>
      var app = angular.module('myApp', []);
    app.controller('MainCtrl', function($scope, $http) {

      $scope.name = "John Brad";

      $scope.important_selection = {
                  "error": false,
                  "client_data": {
                  "important": [
                      {
                          "id": 10,
                          "name": "Socially Responsible"
                      },
                      {
                          "id": 8,
                          "name": "LBGT"
                      },
                      {
                          "id": 4,
                          "name": "Education"
                      },
                      {
                          "id": 2,
                          "name": "Retirement"
                      }
                  ]
                },
                "status_code": 200
              };

    $scope.importants_series = {
                "error": false,
                "important": [
                    {
                        "id": 1,
                        "name": "Investment"
                    },
                    {
                        "id": 2,
                        "name": "Retirement"
                    },
                    {
                        "id": 3,
                        "name": "Insurance"
                    },
                    {
                        "id": 4,
                        "name": "Education"
                    },
                    {
                        "id": 5,
                        "name": "Tax"
                    },
                    {
                        "id": 6,
                        "name": "Estate"
                    },
                    {
                        "id": 7,
                        "name": "Business"
                    },
                    {
                        "id": 8,
                        "name": "LBGT"
                    },
                    {
                        "id": 9,
                        "name": "Offshore"
                    },
                    {
                        "id": 10,
                        "name": "Socially Responsible"
                    },
                    {
                        "id": 11,
                        "name": "Divorce"
                    }
                ],
                "status_code": 200
            };          
    });
    </script>
<select name="important[]" class="form-control" multiple="" required>
        <option ng-selected="important_selection.important.find(item => item.id === important.id)" ng-repeat="important in importants_series.important" value="{{important.id}}">{{important.name}}</option>
    </select>

{{important.name}}
现在我想在数组中搜索id,如果为true,那么它将被选中。 我在
ng中选择了问题

Plunker链接了解更多详细信息


任何帮助都将不胜感激。

您应该在
选择中使用
ng选项
ng模型
,以方便数据绑定

<select name="important[]" class="form-control" multiple="" required 
    ng-options="important.name for important in importants_series.important" 
    ng-model="important_selection.client_data.important"></select>
或者您可以将其分配给一个新数组