How can I update scope in directive?
<div ng-controller="MyCtrl">
<p t></p>
</div>
My directive:
var myModule = angular.module('myModule', [])
.directive('t', function () {
return {
template: '{{text}}',
link: function (scope, element, attrs) {
scope.text = '1';
element.click(function() {
scope.text = '2';
});
}
};
})
.controller('MyCtrl', ['$scope', function ($scope) {
}]);
After click directive does not update.
Use
$apply
method:Explanation: How does data binding work in AngularJS?