Angular JS
Manipulating the DOM from your directive is fairly simple.
You just have to use
link
function instead of controller
into your directive.
Inside directives, it is best practice to use controller only when you want to share
functions with other directives. All other times you should use link.
card.js
angular.module('myApp')
.directive('myCard', function() {
return {
restrict: 'E',
templateUrl: './templates/directives/my-card.html',
scope: {
header: '=',
description: '=',
dealed: '='
},
link: function(scope, element) {
if (scope.dealed) {
element.addClass('dealed');
}
}
};
});
Directives should Clean Up after themselves (A clean DOM is a happy DOM)
Best Practice:
angular.module('myApp')
.directive("title", function() {
return function(scope, element, $timeout) {
...
scope.$on('$destroy', function() {
element.tooltip('destroy');
});
};
});
Destroy method is called whenever the directive is removed (off hover) and its scope is destroyed.
Creating, Using and Destroying attribute Directive:
*.js
angular.module('MyApp')
.directive('popover', function($timeout) {
return function(scope, element, attr) {
$timeout(function(){
element.popover({trigger: 'hover', content: attr.popover});
});
// clean up
scope.$on('$destroy', function() {
element.popover('destroy');
});
};
});
*.html
<div class="card" popover="{{note.title}} {{note.createdDate | date: 'MMM. dd'}}">
<h2 class="h3">{{note.title}}</h2>
<p>{{note.createdDate | date: "'Created on a' EEEE 'in' MMMM"}}</p>
</div>
see Also
- Angular Recipe: Passing data into your Directive
- Angular Recipe:Insert html string from js into html (solve Error: [$sce:unsafe])
- Keynote: AngularJS Filters
- Keynote: AngularJS Services
- Keynote: AngularJS Routing
- Keynote: AngularJS Do the custom things
- Keynote: AngularJS Do the regular things
- Keynote: AngularJS Basics
- Bootstrap Components
- AngularJS - API
- AngularJS - Developer Documentation
- AngularJS - Receipes with Angular
- AngularJS - Receipes github
- Keynote: Top JS frmeworks 2015
Nice Blog , This is what I exactly Looking for , Keep sharing more blog .
ReplyDeleteHire Angular Developer in India
AngularJS Institute in Gurgaon
ReplyDelete