Answer the question
In order to leave comments, you need to log in
Why doesn't $digest() work in angularjs?
HTML
<div ng-controller="MainCtrl">
<div class="btn-group" tri-button counter="buttons.totalClick" >
<button class="btn btn-default" ng-repeat="name in buttons.names">{{name}}</button>
</div>
<h5>{{buttons.totalClick}}</h5>
</div>
// app.js
var TestApp = angular.module('TestApp', []);
// controller.js
angular.module('TestApp')
.controller('MainCtrl', ['$scope', function ($scope) {
$scope.buttons = {
names:['Button #1','Button #2','Button #3'],
totalClick:0
}
}]);
//directive.js
angular.module('TestApp')
.directive('triButton', [function () {
return {
scope:{counter:'=counter'},
link: function (scope, elem, attr) {
elem.on('click',function(event){
scope.counter++
scope.$digest();
console.log(scope.counter)
})
}
};
}])
elem.on('click',function(event){
scope.$apply(scope.counter++)
console.log(scope.counter)
})
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question