AngularJS ng-selected Issue -
i have multi-select. have 2 two different arrays. want multi-select comes selected values.
i want search ng-repeat id
array if found becomes true , value come selected.
my angularjs code
<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>
my html code
<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>
now want search id in array , if true selected. have issue in ng-selected
plunker link more details
: https://plnkr.co/edit/bununqr0ievj5bkslznm?p=preview
any appreciating thanks.
you should using ng-options
, ng-model
in select
easy data binding.
<select name="important[]" class="form-control" multiple="" required ng-options="important.name important in importants_series.important" ng-model="important_selection.client_data.important"></select>
unfortunately ng-model
checks object reference equality, need additional step reassigning data selection array itself
var oldarray = $scope.important_selection.client_data.important; $scope.important_selection.client_data.important = []; oldarray.foreach(function(item) { var refitem = $scope.importants_series.important.find(function(i) { return i.id == item.id; }); $scope.important_selection.client_data.important.push(refitem); });
or can assign new array instead.
Comments
Post a Comment