Assume we need to create two ways to define configs of a directive:
假设我们需要创建两种方法来定义指令的配置:
1 - using element attributes
1 - 使用元素属性
<any main-dir main-name="myname" name-id="may-id" main-other-config="other config"></any>
2- requiring another directive
2-需要另一个指令
app.directive("mainDirConfig", function () {
return {
controller: function (scope){
scope.config = {
name: 'my name',
id: 'my-id',
otherConfigs: 'other config'
}
}
}
});
<any main-dir main-dir-config></any>
how to require mainDirConfig
directive inside mainDir
directive (as a preferred way) only if mainDirConfig
exists and otherwise use element attributes as configs?
如果mainDirConfig存在,如何在mainDir指令内部(作为首选方式)需要mainDirConfig指令,否则使用元素属性作为配置?
More info: I want to use this config for an external module and I need to separate user configs from module.
更多信息:我想将此配置用于外部模块,我需要将用户配置与模块分开。
1 个解决方案
#1
1
From the angular documentation here:
从这里的角度文档:
require
Require another directive and inject its controller as the fourth argument to the linking function. The require takes a string name (or array of strings) of the directive(s) to pass in. If an array is used, the injected argument will be an array in corresponding order. If no such directive can be found, or if the directive does not have a controller, then an error is raised. The name can be prefixed with:
(no prefix) - Locate the required controller on the current element. Throw an error if not found.
? - Attempt to locate the required controller or pass null to the link fn if not found.
^ - Locate the required controller by searching the element and its parents. Throw an error if not found.
^^ - Locate the required controller by searching the element's parents. Throw an error if not found.
?^ - Attempt to locate the required controller by searching the element and its parents or pass null to the link fn if not found.
?^^ - Attempt to locate the required controller by searching the element's parents, or pass null to the link fn if not found.
It is mentioned in the directive section but only fully defined in the $compile section.
它在指令部分中提到,但仅在$ compile部分中完全定义。
#1
1
From the angular documentation here:
从这里的角度文档:
require
Require another directive and inject its controller as the fourth argument to the linking function. The require takes a string name (or array of strings) of the directive(s) to pass in. If an array is used, the injected argument will be an array in corresponding order. If no such directive can be found, or if the directive does not have a controller, then an error is raised. The name can be prefixed with:
(no prefix) - Locate the required controller on the current element. Throw an error if not found.
? - Attempt to locate the required controller or pass null to the link fn if not found.
^ - Locate the required controller by searching the element and its parents. Throw an error if not found.
^^ - Locate the required controller by searching the element's parents. Throw an error if not found.
?^ - Attempt to locate the required controller by searching the element and its parents or pass null to the link fn if not found.
?^^ - Attempt to locate the required controller by searching the element's parents, or pass null to the link fn if not found.
It is mentioned in the directive section but only fully defined in the $compile section.
它在指令部分中提到,但仅在$ compile部分中完全定义。