指令


基本用法

<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. EasyUI 调用getSelections方法只能获取到一行的原因

    $('#tt').datagrid({ url: 'GetDataJosn', title: 'DataGrid', width: 800, height: 300, pageSize: 10, id ...

  2. 调试技巧 —— 如何利用windbg + dump + map分析程序异常

    调试技巧 —— 如何利用windbg + dump + map分析程序异常 逗比汪星人2011-09-04上传   调试技巧 —— 如何利用windbg + dump + map分析程序异常 http ...

  3. JavaScript日期时间操作

    <script> var d=new Date();//当前时间 alert(d); var d1=new Date(1992,5,19);//定义一个时间,月份要加1; alert(d1 ...

  4. Beta Round #9 (酱油杯noi考后欢乐赛)PLQ和他的小伙伴们

    题目:http://www.contesthunter.org/contest/Beta%20Round%20%EF%BC%839%20%28%E9%85%B1%E6%B2%B9%E6%9D%AFno ...

  5. 老的acm & oj学习站点

    1.网易小鱼博客 http://gisyhy.blog.163.com/blog/#m=0&t=1&c=fks_087069086082087064085081082095085084 ...

  6. (转载)c库不正确问题

    (转载)http://blog.csdn.net/piratejk/article/details/6115748 在linux下面变成,有时候在一个发行版本上编译通过,并且可以运行,但是将程序拷贝到 ...

  7. 进军es6(2)---解构赋值

    本该两周之前就该总结的,但最近一直在忙校招实习的事,耽误了很久.目前依然在等待阿里HR面后的结果中...但愿好事多磨!在阿里的某轮面试中面试官问到了es6的掌握情况,说明es6真的是大势所趋,我们更需 ...

  8. EJB (not bound)

    问题: 在代码实在找不到错误的情况下,仍然报:XXXX not bound 问题产生过程: 通过下图方式创建的项目:EJBTest2_1 勾选下面两项,即可生成:EJBTest2_1EJB 和 EJB ...

  9. noip2011 公交观光

    描述 风景迷人的小城Y市,拥有n个美丽的景点.由于慕名而来的游客越来越多,Y市特意安排了一辆观光公交车,为游客提供更便捷的交通服务.观光公交车在第0分钟出现在1号景点,随后依次前往2.3.4……n号景 ...

  10. 【转】[慢查优化]联表查询注意谁是驱动表 & 你搞不清楚谁join谁更好时请放手让mysql自行判定

    转自:http://zhengyun-ustc.iteye.com/blog/1942797 写在前面的话: 不要求每个人一定理解 联表查询(join/left join/inner join等)时的 ...