Pankaj Parkar
Asp.Net + AngularJS Developer
TSS Consultancy
Example.
<div>
Hello, I'm <input ng-model="name">
<span ng-bind="name"><span>
<span ng:bind="name"><span>
<span ng_bind="name"><span>
<span data-ng-bind="name"><span>
<span x-ng-bind="name"><span>
</div>
Templates
ng-disabled
ng-cloak
ng-show
ng-hide
ng-if
ng-repeat
ng-switch
ng-view
Application
ng-app
ng-controller
Data Binding
ng-bind
ng-href
ng-bind-html
ng-href
ng-init
ng-model
Forms
ng-form
ng-minlength
ng-maxlength
ng-pattern
ng-submit
ng-enabled
ng-disabled
ng-readonly
Behaviour
ng-blur
ng-change
ng-click
ng-focus
ng-mouseenter
ng-mouseout
ng-keyup
ng-keydown
app.directive('myDirective', function(){
return {
restrict: 'AECM', //defaults to a
template: '<div>My HTML</div>',
//templateUrl: 'myDirective.html',
replace: false //or true,
link: function(){
//code here
}
}
})
Template, restrict & replace option of directive
Directive
Markup
<div my-directive></div>
<my-directive></my-directive>
<div class="my-directive"></div>
<!-- directive: my-directive -->
app.directive('myDirective', function(){
return {
restrict: 'AECM',
template: '<div>My HTML</div>',
replace: false,
compile: function(element, attrs){
//it is raw DOM, gets called while initializing DOM
//its a DOM which going for compilation, here we don't have scope available
//here its safe to manipulate DOM before it gets transform
return {
pre: function(scope, element, attrs, ctrl){
//this function has scope property to change in scope variables if any,
//this gets called when binding the scope to the DOM
//it has scope available in it, used in some special cases
//like binding some event/watcher before rendering of inner content
},
post: function(scope, element, attrs, ctrl){
//here you can find all the bindings are in place
//generally you should use this function while dealing with DOM.
//because inner content has been rendered.
}
}
}
});
app.directive('myDirective', function(){
return {
restrict: 'AECM',
template: '<div>My HTML</div>',
replace: false,
compile: function(element, attrs){
console.log("Get called first");
return {
pre:{
console.log("Get called third");
},
post:{
console.log("Get called forth");
},
}
},
controller: function(){
console.log("Get called 2nd");
}
});
There are 3 types of scope that you can use in angular directive
Text
app.directive('myDirective', function(){
return {
restrict: 'AECM',
template: '<div>My HTML</div>',
replace: false,
link: function(scope, element, attrs){
console.log("This is nothing but post link");
},
controller: function(){
console.log("Controller get instantiated");
},
priority: '1',
controllerAs: 'alias'
}
})
Photoframe
Angular2 Simple App in Plunkr