Skip to content

Instantly share code, notes, and snippets.

@savokiss
Created February 8, 2017 05:45
Show Gist options
  • Select an option

  • Save savokiss/df7f0e741447179be6765330f91c980e to your computer and use it in GitHub Desktop.

Select an option

Save savokiss/df7f0e741447179be6765330f91c980e to your computer and use it in GitHub Desktop.
指令的生命周期
<body ng-controller="DemoController as demo">
<div id="directiveLife">
<directive-life count="1">
</directiveLife>
</div>
</body>
angular.module('com.ngnice.app').directive('directiveLife', function($log){
$log.info('Injecting function directiveLife');
return {
restrict: 'EA',
transclude: true,
replace: true,
template: '<div><h2>count: {{count}}</h2><p ng-transclude></p></div>',
scope: {
count: '='
},
compile: function(elm, iAttrs){
$log.info('compile', 'count value from attribute : ' + iAttrs.count);
return {
pre: function(scope, elm, iAttrs){
$log.info('pre-link', 'count value from attribute : ' + iAttrs.count, 'count value from scope : ' + scope.count);
},
post: function(scope, elm, iAttrs){
$log.info('post-link', 'count value from attribute : ' + iAttrs.count, 'count value from scope : ' + scope.count);
}
};
},
controller: function($scope){
$log.info('controller', 'count value from controller : ' + $scope.count);
}
};
});
angular.module('com.ngnice.app').controller('DemoController', function(){
var vm = this;
return vm;
});
// Injection function directiveLife
// compile count value from attribute: 1
// controller count value from controller: 1
// pre-link count value from attribute: 1 count value from scope: 1
// post-link count value from attribute: 1 count value from scope: 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment