指令


基本用法

<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.2/angular.js"></script>
<div ng-controller="detail">
<input type="text" ng-model="test">
<label ui-a="test"></label>
</div>
<script>
var app = angular.module('app', [])
.directive('uiA',function() {
return function(scope, element, attr) {
element.addClass('ng-binding').data('$binding', attr.uiA);
scope.$watch(attr.uiA,function uiAWatchAction(value) {
element.html(value + "directive" || '');
});
}
}).controller('detail', ['$scope',function($scope) {
$scope.test = 1
}]);
angular.bootstrap(document, ['app']);
</script>
</html>

这个是最基本的用法其中的指令匹配遵循以下原则

  1. 使用指令时可以在元素或者属性的前面可以添加x-data-
  2. 忽略:,-,_分隔符,并且忽略大小写

或者用$compileProviderdirective方法创建指令,方法如下

angular.module('app', []).config(['$compileProvider',
function($compileProvider) {
$compileProvider.directive({
uiA: function() {
return function(scope, element, attr) {
element.addClass('ng-binding').data('$binding', attr.uiA);
scope.$watch(attr.uiA,
function uiAWatchAction(value) {
element.html(value || '');
});
}
}
});
}]);

其中scope.$watch方法是监听对象的属性是否改变,里面的三个参数为

  1. scope 是angularjs作用域对象
  2. element 是指令匹配的jqlite对象
  3. attrs 是存放着这个对象的属性和属性操作的对象

高级用法(上面都是返回方法,这次是返回对象)


angular.module('app', []).config(['$compileProvider',
function($compileProvider) {
$compileProvider.directive({
uiA: function() {
return {
priority: 0,
terminal:true,
template:'',
templateUrl:'',
restrict:'AE',
scope: {
customer: '='
},
replcae:true,
link: {
pre: function preLink(scope, iElement, iAttrs, controller) { },
post: function postLink(scope, iElement, iAttrs, controller) {
iElement.addClass('ng-binding').data('$binding', iAttrs.uiA);
scope.$watch(iAttrs.uiA,function uiAWatchAction(value) {
element.html(value || '');
});
}
},
compile: function compile(tElement, tAttrs, transclude) {
return {
pre: function preLink(scope, iElement, iAttrs, controller) { ... },
post: function postLink(scope, iElement, iAttrs, controller) { ... }
}
},
require:'',
transclude:true,
controlleras:'a',
controller: function($scope, $element, $attrs){ }
}
}
});
}]);
  1. priority权重默认是0,当包含多个指令的时候按照权重从大开始,其中Pre-link方法为从大到小,
    post-link方法从小到大。当权限相同的时候目前测试结果是按照顺序
  2. terminal当设置为true的时候比当前权重低的指令会失效,相同的会执行
  3. templatetemplateUrl的功能和前面的路由里面的一样
  4. replcaetrue会替换当前指令的元素,false会替换元素innerHTML属性,
    使用的时候模板必须有根节点,感觉只有restrictE时有效
  5. restrict,AE即匹配属性和元素名称,其中A为仅匹配属性,E为仅匹配元素名称,M匹配注释
    <html>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.2/angular.js"></script>
    <div g-controller="detail">
    <ui-a info="test"></ui-a>
    </div>
    <script>
    angular.module('app', []).config(['$compileProvider',
    function($compileProvider) {
    $compileProvider.directive({
    uiA: function() {
    return {
    restrict: 'E',
    replace:true,
    scope: {
    info: '=info'
    },
    template:'<div>{{info.name}},{{info.sex}}</div>'
    }
    }
    });
    }]).controller('detail', ['$scope',
    function($scope) {
    $scope.test = {name:'a',sex:'1'};
    }]);
    angular.bootstrap(document, ['app']);
    </script>
    </html>
  6. scope设置绑定指令的上下文,使用的时候必须要restrictE,
    当设置为true的时候指令之间是不共用一个上下文,应该默认为false

    1. {info: '=info'}获取属性为info的对象
    2. {info: '@info'}获取属性为info的值并且标签里面写成info="{{test}}
    3. {info: '&info'}里面写表达式,并且可以赋予参数
      <html>
      <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.2/angular.js"></script>
      <div ng-controller="detail">
      {{count}}
      <ui-a info="count = count + amount"></ui-a>
      </div>
      <script>
      angular.module('app', []).config(['$compileProvider',
      function($compileProvider) {
      $compileProvider.directive({
      uiA: function() {
      return {
      restrict: 'E',
      scope: {
      info: '&info'
      },
      controller:function($scope){
      $scope.info({amount:11});
      }
      }
      }
      });
      }]).controller('detail', ['$scope',
      function($scope) {
      $scope.count =1;
      }]);
      angular.bootstrap(document, ['app']);
      </script>
      </html>
  7. controller为当前指令中的控制器对象,里面的第四个参数$transclude可以
    设置transclude的元素和compile的第三个参数一样
    4.transclude当设置为ture的时候指令原来的html插入到有ng-transclude的属性的元素,
    默认为false,当设置为element的时候还未知...

    <html>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.2/angular.js"></script>
    <div>
    <ui-a>Check out the contents</ui-a>
    </div>
    <script>
    angular.module('app', []).config(['$compileProvider',
    function($compileProvider) {
    $compileProvider.directive({
    uiA: function() {
    return {
    restrict: 'E',
    transclude: true,
    template: '<div class="alert" ng-transclude></div>'
    }
    }
    });
    }]);
    angular.bootstrap(document, ['app']);
    </script>
    </html>
  8. require用于请求其他的指令,在link的第四个参数注入,
    ^表示在dom模型上面的上一个节点,没有加表示是在同一个元素上面,
    同级的不知何解,当前前面加上?表示不存在的时候不报错,
    调用另一个控制器只能是声明this后的,如以下代码改成$scope.a=1则调用不到
    <html>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.2/angular.js"></script>
    <div>
    <ui-b>
    <ui-a></ui-a>
    </ui-b>
    </div>
    <script>
    angular.module('app', []).config(['$compileProvider',
    function($compileProvider) {
    $compileProvider.directive({
    uiB: function() {
    return {
    restrict: 'E',
    transclude:true,
    controller: function($scope){
    this.a=1;
    },
    template: '<div ng-transclude></div>'
    }
    },
    uiA: function() {
    return {
    require: '^uiB',
    restrict: 'E',
    link: function(scope, element, attrs,uiBCtrl) {
    console.log(uiBCtrl.a);
    },
    template: ''
    }
    }
    });
    }]);
    angular.bootstrap(document, ['app']);
    </script>
    </html>
  9. controlleras控制器的别名
  10. link里面的post方法就是前面的基本用法,如果存在compile,则link失效,
    其中pre为连接前,post为连接后
  11. compile主要用来处理模板的DOM ,返回的对象是一个link
<html  ng-app="compile"><script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.2/angular.js"></script>
<div ng-controller="Ctrl">
<input ng-model="name"> <br>
<textarea ng-model="html"></textarea> <br>
<div compile="html"></div>
</div>
<script>
angular.module('compile', [], function($compileProvider) {
$compileProvider.directive('compile', function($compile) {
//新建一个link方法
return function(scope, element, attrs) {
scope.$watch(
function(scope) {
//判断编译的表达式是否改变
return scope.$eval(attrs.compile);
},
function(value) {
//当编译的表达式改变重写
element.html(value);
//获得控制器的上下文以及表达式重新编译
$compile(element.contents())(scope);
}
);
};
})
}); function Ctrl($scope) {
$scope.name = 'Angular';
$scope.html = 'Hello {{name}}';
}
</script>
</html>

Ⅵ.AngularJS的点点滴滴-- 指令的更多相关文章

  1. Ⅶ.AngularJS的点点滴滴-- 事件

    事件(和js一样有冒泡和捕获) <html> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2 ...

  2. Ⅴ.AngularJS的点点滴滴-- 资源和过滤

    资源ngResource(依赖ngResource模块) <html> <script src="http://ajax.googleapis.com/ajax/libs/ ...

  3. Ⅲ.AngularJS的点点滴滴-- 路由

    路由ngRoute (需要依赖ngRoute模块) <html> <script src="http://ajax.googleapis.com/ajax/libs/ang ...

  4. Ⅰ.AngularJS的点点滴滴--引导

    AngularJS已经被很多人像炒冷饭一样炒过啦,大部分都是直接复制官方文档没有说明一些注意事项,不过什么都要从头开始吧 页面引导实例化 1.自动实例化 <html> <script ...

  5. AngularJS中的指令全面解析(转载)

    说到AngularJS,我们首先想到的大概也就是双向数据绑定和指令系统了,这两者也是AngularJS中最为吸引人的地方.双向数据绑定呢,感觉没什么好说的,那么今天我们就来简单的讨论下AngularJ ...

  6. 使用Angularjs的ng-cloak指令避免页面乱码

    在使用Anguarjs进行web开发或者进行SPA(single page application)开发时,往往会遇到下面这样的问题. 刷新页面时,页面会出现一些乱码,这里的乱码具体是指`{{expr ...

  7. 带你走近AngularJS - 创建自定义指令

    带你走近AngularJS系列: 带你走近AngularJS - 基本功能介绍 带你走近AngularJS - 体验指令实例 带你走近AngularJS - 创建自定义指令 ------------- ...

  8. AngularJS中Directive指令系列 - scope属性的使用

    文章是转的,我做下补充.原文地址:https://segmentfault.com/a/1190000002773689 每当一个指令被创建的时候,都会有这样一个选择,是继承自己的父作用域(一般是外部 ...

  9. 你知道用AngularJs怎么定义指令吗?

    前言 最近学习了下angularjs指令的相关知识,也参考了前人的一些文章,在此总结下. 欢迎批评指出错误的地方.   Angularjs指令定义的API AngularJs的指令定义大致如下 ang ...

随机推荐

  1. 第三章 传奇的开始--Delphi(附读书笔记)

    第三章 传奇的开始--Delphi "是惊世之作的Delphi让Borland重新站了起来,没有当初的Delphi,就没有今日的Borland!" "是Turbo Pas ...

  2. bower初接触

    之前从Steve Sanderson的博文Architecting large Single Page Applications with Knockout.js中学习了用Yeoman创建Knocko ...

  3. Hadoop开发环境搭建

    hadoop是一个分布式系统基础架构,由Apache基金会所开发. 用户可以在不了解分布式底层细节的情况下,开发分布式程序.充分利用集群的威力高速运算和存储.   Hadoop实现了一个分布式文件系统 ...

  4. 【HDOJ】2133 What day is it

    需要注意数据有效性. #include <stdio.h> #define isLeapYear(y) (y%4==0&&y%100!=0)||(y%400==0) ][] ...

  5. 【转】win7 虚拟机virtualbox中ubuntu12.04安装samba实现文件共享

    原文网址:http://blog.csdn.net/watkinsong/article/details/8878786 昨天心血来潮,又装了个虚拟机,然后安装了ubuntu12.04,为了实现在虚拟 ...

  6. Android Fragment类方法

    public void onStart() 当该Fragment对象对用户可见时,该方法会被调用.该方法通常会跟它的Activity的生命周期的Activity.onStart()方法绑定. publ ...

  7. python三元运算符

    在c.php里面,都有三元运算符,如:   a = b?c:d 意思是 b 的运算结果如果是True,那么整个表达式a就等于c,反之如果b的运算结果是False,那么a就等于d. 这样写起来简洁又高效 ...

  8. visual studio 资源视图 空白 解决方案

    visual studio 资源视图打开后显示空白的解决方案步骤: 在解决方案view下,右键点击工程 1 unload projects 完成第一步后仍然在解决方案view下,右键点击工程 2 re ...

  9. Cocos2d-x java 通过jni调用c++的方法

    前面博客说到,cocos2d-x c++界面层运行在一个GLThread线程里面,严格的说是运行在Cocos2dxGLSurfaceView(继承自GLSurfaceView) 里面.opengl的渲 ...

  10. java NIO 资料总结

    1.http://developer.51cto.com/art/201204/328340.htm 2.http://ifeve.com/file-channel/并发编程网系列 3 http:// ...