Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angularjs: select not updating when ng-model is updated

I've created the following example so you can see exactly what is happening: http://jsfiddle.net/8t2Ln/101/

The same thing happens if I use ng-options. I have a different reason for doing it this way, but for the simplification of the example left that part out.

As you can see it has by default two options. I'm displaying the selected value of the ng-model next to the select so you can see what it is. When you use the top piece to add a third option it sets the value to the value of that new option as is evidenced by the displayed ng-model value next to the select, but the select itself doesn't change to show the correct value selected.

Below is the sample code at the link:

var testApp = angular.module('testApp', ['ngRoute']);  testApp.controller('Ctrl', function ($scope) {      $scope.newInput = '';     $scope.inputDevice = [         {             value: '1',             label: 'input1'         },         {             value: '2',             label: 'input2'         }     ];     $scope.selectedDevice = '';      $scope.addType = function () {         var newElem = {             label: $scope.newInput,             value: '3'         };         $scope.inputDevice.push(newElem);         $scope.selectedDevice = newElem.value;     };   }); 

And here is the html:

<div ng-app="testApp">     <div ng-controller="Ctrl">         <p>             <input type="text" ng-model="newInput" />             <br />             <button ng-click="addType()">Add Type</button>         </p>         <select ng-model="selectedDevice">             <option></option>             <option ng-repeat="i in inputDevice" value="{{ i.value }}">{{ i.label }} - {{ i.value }}</option>         </select>         {{ selectedDevice }}</div> </div> 
like image 231
Jhorra Avatar asked Oct 06 '14 07:10

Jhorra


People also ask

How do I set default selected value in ng-options?

Use ng-init to set default value for ng-options . Save this answer.

How does ng select work?

The ng-selected directive sets the selected attribute of an <option> element in a <select> list. The option will be selected if the expression inside the ng-selected attribute returns true. The ng-selected directive is necessary to be able to shift the value between true and false .

What is difference between ng-repeat and Ng-options?

ng-options is the directive which is designed specifically to populate the items of a dropdown list. One major advantage using ng-options for the dropdown is, it allows us to pass the selected value to be an object. Whereas, using ng-repeat the selected value can only be string.

What is the difference between ng-model and data NG model?

For AngularJS there is no difference between ng-app and data-ng-app or ng-controller and data-ng-controller or ng-model and data-ng-model because while compiling HTML page, AngularJS strips data- or x- prefix.


1 Answers

This is exactly why you should not use ngRepeat to render select options. You should use ngOptions instead:

<select ng-model="selectedDevice"          ng-options="i.value as (i.label + '-' + i.value) for i in inputDevice">     <option></option> </select> 

In general, avoid using ngRepeat for rendering select options. There are at least two good reasons. ngRepeat creates separate child scope per iteration, which is not needed in case of option tag. Another important caveat is that with ngRepeat you can only bind select to primitives like strings, but you won't be able to write object to ngModel with it.

Here is a demo below.

angular.module('demo', []).controller('DemoController', function($scope) {        $scope.newInput = '';      $scope.inputDevice = [      	{value: '1', label: 'input1'},           {value: '2', label: 'input2'}      ];            $scope.selectedDevice = '';      $scope.addType = function() {          var newElem = {              label: $scope.newInput,              value: Number($scope.inputDevice[$scope.inputDevice.length - 1].value) + 1          };          $scope.inputDevice.push(newElem);          $scope.selectedDevice = newElem.value;          $scope.newInput = '';      };    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular.min.js"></script>  <div ng-app="demo" ng-controller="DemoController">      <form ng-submit="addType()">          <input type="text" ng-model="newInput" />          <button type="submit">Add Type</button>      </form>      <select ng-model="selectedDevice" ng-options="i.value as (i.label + ' - ' + i.value) for i in inputDevice">          <option>Select</option>      </select>      {{ selectedDevice }}  </div>
like image 183
dfsq Avatar answered Sep 20 '22 23:09

dfsq