如果存在,如何要求指令(在另一个指令内)

Rey*_*raa 5 javascript angularjs angularjs-directive

假设我们需要创建两种方法来定义指令的配置:

1 - 使用元素属性

<any main-dir main-name="myname" name-id="may-id" main-other-config="other config"></any>
Run Code Online (Sandbox Code Playgroud)

2-需要另一个指令

app.directive("mainDirConfig", function () {
  return {
    controller: function (scope){
      scope.config = {
        name: 'my name',
        id: 'my-id',
        otherConfigs: 'other config'
      }
    }
  }
});
Run Code Online (Sandbox Code Playgroud)


<any main-dir main-dir-config></any>
Run Code Online (Sandbox Code Playgroud)

如果需要mainDirConfig指令指令内部mainDir指令(作为首选方式),如果mainDirConfig存在,否则使用元素属性作为配置?

更多信息:我想将此配置用于外部模块,我需要将用户配置与模块分开.

tes*_*123 1

从这里的角度文档:

要求

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.
Run Code Online (Sandbox Code Playgroud)

它在指令部分中提到,但仅在 $compile 部分中完整定义。