Sharping Up with Angularjs 1.x -Direactive

1、基本指令声明

eg:
  app.directive('productGallery', function(){
     return {
       restrict: 'E',
       templateUrl: 'product-gallery.html',
       controller: function(){
          this.current = 0;
          this.setCurrent = function(imageNumber){
          this.current = imageNumber || 0;
        };
       },
       controllerAs: 'gallery'
     };
  });

restrict - 声明指令的使用方法:
E: 作为标签使用 <poduct-allery></poduct-gallery>
A: 作为属性使用 <div poductGallery></div>
C: 同类名绑定 <div class="poductGallery"></div>
M: 作为注释使用,不推荐

templateUrl - 声明需要加载的html模板,可以是html文件路径,也可以是一个函数。(可以使用 ng-include 实现动态 templateUrl,但是否更适合使用 ng-route 实现还有待验证)

controller 中可声明形如

this.addStrength = function() {
    $scope.abilities.push("strength");
};

的函数,暴露给其他 direactive 调用

猜你喜欢

转载自blog.csdn.net/u010879111/article/details/52434872