I have a specific problem which I can't seem to crack.
In html I have following code:
<select ng-model="$parent.proizvod" ng-options="i.id_proizvod as i.proizvod for i in proizvodiServer">
<option>Select</option>
</select>
And in my controllers:
$rootScope.proizvod = '1';
So when the page first loads, the first option is selected. If I change the $rootScope.proizvod = '1'; to $rootScope.proizvod = '3'; It will load the third option.
Now, when you select some other value, it will store it in $rootScope.proizvod, but later, in some function, I want to reset that $rootScope.proizvod back to '1' and it doesn't seem to be working.
What am I missing here?
P.S. without $parent.proizvod, my select doesn't even change the $rootScope.proizvod value.
EDIT: Here's the other fucntion:
$rootScope.upisPodataka = function() {
setTimeout(function () {
$rootScope.proizvod = '1';
$scope.$apply();
}, 2000);
$state.go('app.podesenost_grilla', {}, {
reload: true
});
upisPodataka function is called on a click of a button.
Using
ng-model='$parent.proizvod'is not guaranteed to update$rootScopein all circumstances.If the
ng-modelsets the value in a child scope. The child scope gets its own property that hides/shadows the parent property of the same name. This is not something AngularJS is doing – this is how JavaScript prototypal inheritance works. Read the Wiki.So the question becomes should it be
$parent.$parent.proizvod? Or maybe$parent.$parent.$parent.proizvod? Do it by trial and error?A more robust approach is to use the ng-change directive to update the variable.
Instead of using
$rootScope, consider using a custom service to store the variables.Use $timeout instead of window.setTimeout
The $timeout service wraps
windows.setTimeoutand integrates it with the AngularJS framework and its digest cycle. Then$apply()is not needed.In addition,
$timeout.flush()can be used when testing.