指令


基本用法

<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. javascript 字符串转数字

    //把str转换为数字的方式,想起很久以前的一个面试题,说字符转数字的方式有哪些,现在想了想 var str1='4.88',str2='4.8xx'; console.log(parseInt(st ...

  2. 断命windows上卸载node并重装

    抠门儿世界500强给前端开发人员用windows windows不支持n模块没法自动升级 不记得何时安装的旧版本node连个uninstaller都找不到 绕道安装nvm path也自动加进去了丫命令 ...

  3. ANDROID_MARS学习笔记_S04_004_用HTTPCLENT发带参数的get和post请求

    一.代码 1.xml(1)activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/r ...

  4. UVA 10716 Evil Straw Warts Live(贪心)

    Problem D: Evil Straw Warts Live A palindrome is a string of symbols that is equal to itself when re ...

  5. Android的十六进制颜色值

    [转自]: http://blog.csdn.net/fxtxz2/article/details/7598256 颜色和不透明度 (alpha) 值以十六进制表示法表示.任何一种颜色的值范围都是 0 ...

  6. (转载)javascript转自世纪流年blog

    (转载)http://www.cnblogs.com/tfe/articles/164205.html 础知识 2005年5月2日22:25星期一 [ Dhtml ] 有些时候你精通一门语言,但是会发 ...

  7. HDU-2710 Max Factor

    看懂: Max Factor Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  8. oracle的基本信息查询

    oracle查看当前数据库实例名 select name from V$DATABASE; 或者 select SYS_CONTEXT('USERENV','INSTANCE_NAME') from ...

  9. windows apache 配置多个服务站点

    原文 方法一:多个APACHE服务 更改第一个站点的根目录: 在文件Apache2.2/conf/httpd.conf中查找 DocumentRoot 属性,将后面的路径改为你的主站点的路径, 如:D ...

  10. 我的第一篇Markdown博客

    我的第一篇Markdown博客 这是我第一次用Markdown写博客,发现还是比较好用的,加上Marsedit也支持了Markdown的博客预览,博客园也加了Markdown的格式支持,就更加方便了, ...