I'm trying to understand how Angular data binding works. I have created this sample that displays {{values.count}}
inside a span
element:
<body ng-app="testApp" ng-controller="testCtrl as values">
<span>Count = {{values.count}}</span>
<button id="incr">++</button>
</body>
<script>
testApp = angular.module("testApp", []);
testApp.controller("testCtrl", function () {
var values = this;
values.count = 5;
$("#incr").on('click', function () {
values.count += 1;
});
});
</script>
If I click on the button, values.count
is incremented, but the span
is not updated. I can get it to work if I place a ng-click="values.Increment()"
on the button and create a values.Increment
method in the controller, but what is the difference that makes one work and the other not?