I am attempting to modify a directive controller's $scope using ng-click.
There is a simple button in my code:
<button ng-click="showEnterKeyField = !showEnterKeyField">btn {{showEnterKeyField}}</button>
This code functions as expected, displaying (in the button text):
btn true
btn false
btn true
... and so on
However, in the controller we have:
$scope.$watch('showEnterKeyField', function(val) {
console.log(val);
}, true);
Yet, no logs appear in the console when this runs.
Next, I tried running a test()
function within the ng-click
:
<button ng-click="test()">btn {{showEnterKeyField}}</button>
To my surprise, this code behaves the same as the previous one:
btn true
btn false
btn true
... and so on
This time, however, I do see records in the console.
Why is the scope not changing in the first scenario? Thank you.
UPDATE
Here is the implementation of $scope.test()
:
$scope.test = function() {
$scope.showEnterKeyField = !$scope.showEnterKeyField;
};