angularjs - Updating model when selected item is deleted -


so in example have simple select

<select ng-model="mycolor" ng-options="color.name color in colors">     <option value="">-- choose color --</option> </select> 

with 3 colors , button delete last one

<button ng-click="delred()">delete red</button><br/> 

js code looks that

angular.module('selectexample', []) .controller('examplecontroller', ['$scope', function($scope) {   $scope.colors = [     {name:'black'},     {name:'white'},     {name:'red'},   ];   $scope.mycolor = $scope.colors[2]; // red   $scope.delred = function(){             $scope.colors.length = 2;   } }]); 

when red selected , clicking on "delete red " -- choose color -- option selected, model {{mycolor}}

{"name":"red"} 

how can set null (or empty string or "empty" value) in case red selected , deleted in order have coherent value?

why not override mycolor if equal element deleting?

  $scope.delred = function(){     if ($scope.mycolor === $scope.colors[$scope.colors.length - 1]) {       $scope.mycolor = undefined;     }     // imo overriding .length of array delete element bit of hack     $scope.colors = $scope.colors.slice(0, $scope.colors.length - 1);   } 

http://plnkr.co/edit/yeowfcrv9oov4ji8zlmn?p=preview


update: has been reported bug in framework. example select on angular docs shows same behavior above program. there outdated pull request address issue well.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -