Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to require a directive (inside another directive) only if exist

Assume we need to create two ways to define configs of a directive:

1 - using element attributes

<any main-dir main-name="myname" name-id="may-id" main-other-config="other config"></any>

2- requiring another directive

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?

More info: I want to use this config for an external module and I need to separate user configs from module.

like image 725
Reyraa Avatar asked Nov 10 '22 20:11

Reyraa


1 Answers

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.

like image 54
testing123 Avatar answered Nov 14 '22 23:11

testing123