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

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -

java - How to specify maven bin in eclipse maven plugin? -