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.
These prefixes are used to bind the parent scope's methods and properties to the directive scope. There are 3 types of prefixes in AngularJS: '@' – Text binding / one-way binding. '=' – Direct model binding / two-way binding.
No difference. It is same object.
The $scope object used by views in AngularJS are organized into a hierarchy. There is a root scope, and the $rootScope can has one or more child scopes.
The ng-transclude directive facilitates AngularJS to capture everything that is put inside the directive in the markup and use it somewhere in the directive's template. Syntax: <ng-transclude.
Use $apply
method:
element.click(function() {
scope.$apply(function(){
scope.text = '2';
});
});
Explanation: How does data binding work in AngularJS?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With