require参数可以被设置为字符串或数组,字符串代表另外一个指令的名字。require会将控制器注入到其值所指定的指令中,并作为当前指令的链接函数的第四个参数。
字符串或数组元素的值是会在当前指令的作用域中使用的指令名称。指令得require参数的值可以用下面的前缀进行修饰,这会改变查找控制器时的行为:

?     在当前指令中没有找到所需要的控制器,会将null作为传给link函数的第四个参数。

^     指令会在上游的指令链中查找require参数所指定的控制器。

?^    将前面两个选项的行为组合起来,选择地加载需要的指令并在父指令链中进行查找。

没有前缀   指令将会在自身所提供的控制器中进行查找,如果没有找到任何控制器(或具有指定名字的指令)就抛出一个错误。

  例子:

angular.module('myApp',[])
//定义第一个指令 bookList
.directive('bookList',function(){
return {
restrict:'ECAM',
controller:function($scope){
$scope.books=[
{name:'php'},{name:'javascript'},{name:'java'}
];
this.addBook=function(){
$scope.$apply(function(){
$scope.books.push({name:'Angularjs'});
});
}
},
controllerAs:'bookListController',
//这个template中使用了第二个指令bookAdd
template:'<div><ul><li ng-repeat="book in books">{{ book.name }}</li></ul> <book-add></book-add> </div>',
replace:true
}
})
//定义第二个指令 bookAdd
.directive('bookAdd',function(){
return{
restrict:'ECAM',
require:'^bookList', //这是个指令名
template:'<button type="button">添加</button>',
replace:true,
link:function(scope,iElement,iAttrs,bookListController){ //这里引用了bookList指令的控制器
iElement.on('click',bookListController.addBook);
}
}
})
.controller('firstController',['$scope',function($scope){
}])

AngularJS 指令中的require的更多相关文章

  1. angularjs指令中的require赋值含义

    前缀 寻找路劲 没有找到控制器是否抛错? 例如 Link函数中第四个参数 (no prefix) 当前指令的DOM 抛错 tabset 找到的Controller对象 ? 当前指令的DOM 不抛错 ? ...

  2. 【转】angularjs指令中的compile与link函数详解

    这篇文章主要介绍了angularjs指令中的compile与link函数详解,本文同时诉大家complie,pre-link,post-link的用法与区别等内容,需要的朋友可以参考下   通常大家在 ...

  3. angularjs指令中的compile与link函数详解

    这篇文章主要介绍了angularjs指令中的compile与link函数详解,本文同时诉大家complie,pre-link,post-link的用法与区别等内容,需要的朋友可以参考下   通常大家在 ...

  4. angular指令中,require和transclude同时设置为true时的作用

    最近在学习angularJS指令的时候,对指令对象中require属性和transclude属性同时设置为true比较疑惑,于是自己动手测试一下具体差异 index.html: <simple& ...

  5. AngularJs 指令directive之require

    controller的用法分为两种情形,一种是require自定义的controller,由于自定义controller中的属性方法都由自己编 写,使用起来比较简单:另一种方法则是require An ...

  6. angularjs指令中的compile与link函数详解(转)

    http://www.jb51.net/article/58229.htm 通常大家在使用ng中的指令的时候,用的链接函数最多的是link属性,下面这篇文章将告诉大家complie,pre-link, ...

  7. angularjs指令中的compile与link函数详解补充

    通常大家在使用ng中的指令的时候,用的链接函数最多的是link属性,下面这篇文章将告诉大家complie,pre-link,post-link的用法与区别. angularjs里的指令非常神奇,允许你 ...

  8. angularjs指令中scope参数 true、false、{} 的区别详解

    scope 有三个参数 true.false.{} scope 默认是 false,当 scope设置为true时,会从父作用域继承并创建一个新的作用域对象, 按照true .false的反向思维,我 ...

  9. AngularJs -- 指令中使用子作用域

    下面将要介绍的指令会以父级作用域为原型生成子作用域.这种继承的机制可以创建一个隔离层,用来将需要协同工作的方法和数据模型对象放置在一起. ng-app和ng-controller是特殊的指令,因为它们 ...

随机推荐

  1. TCP Nagle算法&&延迟确认机制

    TCP Nagle算法&&延迟确认机制 收藏 秋风醉了 发表于 3年前 阅读 1367 收藏 0 点赞 0 评论 0 [腾讯云]买域名送云解析+SSL证书+建站!>>> ...

  2. C++动态链接库

    1.动态链接库概述: 动态链接库通常都不能直接运行,也不能接受消息:只有在其他模块调用动态链接库中的函数时,它才发挥作用. Windows API中所有的函数都包含在动态链接库中. 动态链接库分静态库 ...

  3. NC 6系后台调用接口保存单据

    IPFBusiAction ipf = (IPFBusiAction)NCLocator.getInstance().lookup(IPFBusiAction.class); ipf.processA ...

  4. How to use external classes and PHP files in Laravel Controller?

    By: Povilas Korop Laravel is an MVC framework with its own folder structure, but sometimes we want t ...

  5. swift MD5 加密方法

    引入OC类库 md5.h: #import <UIKit/UIKit.h> @interface Md5Controller : UIViewController @end md5.m: ...

  6. unwind

    unwind:可以将一个列表展开为一个行的序列1.列表 unwind[1,2,3]as x return x2.创建唯一列表with[1,2,3,3]as coll unwind coll as x ...

  7. PDF下载网

    http://www.java1234.com/a/javabook/javaweb/2018/1103/12297.html

  8. Trapping Rain Water LT42

    The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of ...

  9. 【UI测试】--帮助设施

  10. json ubuntu下安装

    1.首先安装scons scons是linux下的自动构建工具,类似cmake. 下载地址wget http://prdownloads.sourceforge.net/scons/scons-2.2 ...