https://github.com/lianbinghua

angular 自定义指令 link or controller

  • 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
posted @ 2015-09-23 11:29  连冰华  阅读(248)  评论(0编辑  收藏  举报