javascript - Angular's NgClick has outdated scope -
i'm experiencing strange behaviour, don't know who's fault is.
i have 3 variables in $scope can changed user. bind select object through ng-model.
<select ng-options="hour.label hour in hours track hour.val" ng-model="new_hour"></select> hh : <select ng-options="minute.label minute in minutes track minute.val" ng-model="new_minute"></select> mm : <br /> new_hour = {{ new_hour }}<br /> new_minute = {{ new_minute }} </br> where variables initialized follows:
$scope.minutes = []; for(var = 0; < 60; ++i) { var min = string(i); if(i < 10) min = "0" + min; $scope.minutes.push({ val: i, label: min}); } $scope.hours = $scope.minutes.slice(0, 24); $scope.new_minute = $scope.minutes[0]; $scope.new_hour = $scope.hours[0]; whenever choose different time, values of $scope.new_hour , $scope.new_minute change expected.
however, have button has ngclick , calls function in $scope.
<button ng-click="add_value()">add value</button> if select new (minute) value , click on button, $scope function sees old values , not new ones:
$scope.add_value = function() { console.log("new_minute: " + $scope.new_minute.val); } let's i've chosen 03 , click on button. console shows new_minute: 0.
however, if modify code:
<button ng-click="add_value(new_minute.val)">add vaue</button> and
$scope.add_value = function(new_minute) { console.log("new_minute: " + new_minute); } then value passed add_value correct. if open inspect console , inspect value of $scope.new_minute.val, $scope.new_minute.val differs local variable new_minute.
i've prepared fiddle basic structure of code: https://jsfiddle.net/shaoran/tnvou6ud/1/
there works expected, cannot reproduce behaviour. has idea might going on?
Comments
Post a Comment