指令


基本用法

<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. js 模块化

    http://kb.cnblogs.com/page/132461/ http://www.adequatelygood.com/JavaScript-Module-Pattern-In-Depth. ...

  2. ThinkPHP3.1快速入门(3)查询语言

    http://www.thinkphp.cn/info/115.html 介绍 ThinkPHP内置了非常灵活的查询方法,可以快速的进行数据查询操作,查询条件可以用于读取.更新和删除等操作,主要涉及到 ...

  3. CAS单点登录配置[3]:服务器端配置

    在准备工作,证书生成等工作完成后,本篇介绍服务器端的配置. JDK配置 1 我们将生成的cacerts文件分别拷贝到JDK目录下的jre/lib/security目录下及JRE对应的目录中,如果之前存 ...

  4. Android之最简单的ImageView加边框方法

    转自:http://www.th7.cn/Program/Android/201301/120345.shtml 通常情况下,如果我们要给ImageView加上边框,比如宽为3dp的灰色框,是自己定义 ...

  5. Oracle core02_数据块

    数据更改 oracle core完成了oracle的核心功能,recovery,读一致性等. 深入的了解oracle的机制,就从一个最简单的更新开始.对于oracle来说,最大的一个特性就是写了两次数 ...

  6. bzoj2631 3282

    这两题都是link cut tree的裸题之前看Qtree的论文,只会在确定父子关系的情况下连边和删边如果在任意两个点连边删边怎么做呢?这时候我们不能随意的将一个点的父节点设为另一个点,因为其中某个点 ...

  7. java基于xml配置的通用excel单表数据导入组件(四、DAO主处理类)

    package XXXXX.manage.importexcel; import java.beans.IntrospectionException; import java.io.BufferedR ...

  8. GeoServer基础教程(一):环境搭建篇

    转自:http://imxz.me/tech/3sdev/installation-of-geoserver.html GeoServer的是一个基于Java的软件,它允许用户查看和编辑地理空间数据, ...

  9. HDU 5961 传递 【图论+拓扑】 (2016年中国大学生程序设计竞赛(合肥))

    传递 Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)     Problem ...

  10. Tag file directory /struts-tags does not start with "/WEB-INF/tags"

    使用自定义标签,记得引用路径 <%@taglib prefix="s" uri="/struts-tags" %>