I am working on a more complex project(10000+ LoC) and during development I encountered a problem very often.
When I add a simple variable to scope like: $scope.editing = true;
$sope.editing=false;
$scope.changeTextButtonClicked = function() {
$scope.editText;
}
<span ng-hide="editing">{{editText}}</span>
<input type="text" ng-model="editText" ng-show="editing">
<input type ="button" ng-click="changeTextButtonClicked()">
The current value of $scope.editText
can't be accessed from the controllers javascript code!
In some cases I got the old value when accessing $scope.editText
like the DOM is not updated but when printing it directly on the website with {{editText}}
it works and gets updated but in the controller I get the old value.
It happened that I have one value for {{editText}}
on the website and another value for $scope.editText
in the controller (invalid one - previous).
I solved this with adding it to a data array or any other array that is nested inside the scope:
$scope.data={}
$scope.data.editText = ...;
Can anybody explain to me why it works sometimes with $scope.variable
and sometime it doesn't and you need to add $scope.**foobar**.variable
?