javascript - Why ng-model does not update controller value select? -


this code html:

<div ng-controller="selectctrl">     <p>selected item : {{selecteditem}}</p>     <p> age of selected item : {{selecteditem.age}} </p>     <select ng-model="selecteditem" ng-options="item.name item in items">     </select> </div> 

this code angularjs:

var app = angular.module('myapp', []);  app.controller('selectctrl', function($scope) {     $scope.items = [{name: 'one', age: 30 },{ name: 'two', age: 27 },{ name: 'three', age: 50 }];     $scope.selecteditem = $scope.items[0];     console.log($scope.selecteditem); //it's not update :( }); 

in view new value updated every time change select, controller not update current value of select. should do?

thanks!

to updated or change value inside controller, write ng-change function on it. check updated value inside controller.

markup

<select ng-model="selecteditem" ng-options="item.name item in items"   ng-change="changeselecteditem()"> </select> 

code

$scope.changeselecteditem = function(){    console.log($scope.selecteditem); } 

other way could use {{selecteditem}} on html somewhere. give idea how selecteditem value updating.


Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -