angular 自定义指令 link or controller

时间:2023-03-08 17:36:45
  • Before compilation? – Controller
  • After compilation? – Link
var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
$scope.name = 'First ';
}); app.directive('exampleDirective', function() {
return {
restrict: 'E',
template: '<p>Hello {{name}}!</p>',
controller: function($scope, $element){
$scope.name = $scope.name + "Second ";
},
link: function(scope, el, attr) {
scope.name = scope.name + "Third ";
}
}
}) 结果:Hello First Second Third !
  • “Am I just doing template and scope things?” – goes into controller
  • “Am I adding some coolbeans jquery library?” – goes in link