zoukankan      html  css  js  c++  java
  • AngularJS自定义指令及指令配置项

    两种写法

    //第一种
    angular.module('MyApp',[])
    .directive('zl1',zl1)
    .controller('con1',['$scope',func1]);
    
    function zl1(){
       var directive={
           restrict:'AEC',
          template:'this is the it-first directive',
        };
        return directive;
    };
    
    function func1($scope){
        $scope.name="alice";
    }
    
    //第二种
    angular.module('myApp',[]).directive('zl1',[ function(){
      return {
        restrict:'AE',
        template:'thirective',
        link:function($scope,elm,attr,controller){
          console.log("这是link");
        },
        controller:function($scope,$element,$attrs){
          console.log("这是con");
        }
      };
    }]).controller('Con1',['$scope',function($scope){
      $scope.name="aliceqqq";
    }]);

    指令配置项

    angular.module('myApp', []).directive('first', [ function(){
        return {
            // scope: false, // 默认值,共享父级作用域
            // controller: function($scope, $element, $attrs, $transclude) {},
            restrict: 'AE', // E = Element, A = Attribute, C = Class, M = Comment
            template: 'first name:{{name}}',
        };
    }]).directive('second', [ function(){
        return {
            scope: true, // 继承父级作用域并创建指令自己的作用域
            // controller: function($scope, $element, $attrs, $transclude) {},
            restrict: 'AE', // E = Element, A = Attribute, C = Class, M = Comment
            //当修改这里的name时,second会在自己的作用域中新建一个name变量,与父级作用域中的
            // name相对独立,所以再修改父级中的name对second中的name就不会有影响了
            template: 'second name:{{name}}',
        };
    }]).directive('third', [ function(){
        return {
            scope: {}, // 创建指令自己的独立作用域,与父级毫无关系
            // controller: function($scope, $element, $attrs, $transclude) {},
            restrict: 'AE', // E = Element, A = Attribute, C = Class, M = Comment
            template: 'third name:{{name}}',
        };
    }])
    .controller('DirectiveController', ['$scope', function($scope){
        $scope.name="mike";
    }]);
  • 相关阅读:
    【转】C#中的虚方法
    【转】ASP.NET 2.0中Page事件的执行顺序
    OWC ChartSpace控件的使用
    Ext对基本类型的扩展
    OWC PivotTable的使用方法
    .net中线程同步的典型场景和问题(1)
    python中使用汉字
    如何取消后台线程的执行
    yaffs2根文件系统的构建过程
    Fuck self.delegate = self
  • 原文地址:https://www.cnblogs.com/wxfallstar/p/6743799.html
Copyright © 2011-2022 走看看