How to share access between policies?

3

How can I call a directive within a second policy?

I have a variable x in directive 1 and I want to access this variable x in a directive 2 so I can say that x (diretiva 1) === y (diretiva 2) .

    
asked by anonymous 25.11.2014 / 14:03

3 answers

3

The recommended way is implementing a service that you must inject into the policies / controls where you want to share your variable. (The idea is that you can share the value and control access in a unified way)

A good example can be found in this original Stack Overflow .

    
25.11.2014 / 15:21
3

It is not a good practice, but you can pass directive 1 as required on 1

app.directive('directive1', function(){
    return { ... }
});

app.directive('directive2', function) {
    return {
        require: 'directive1',
        link: function(scope, elem, attrs, directive1Ctrl /* *** AQUI VAI O CONTROLLER DA DIRETIVA1 ** */) {
            // ACESSE A DIRECTIVE 1 PELO QUARTO PARAMETRO PASSADO.
        }
});
    
19.12.2014 / 14:16
0

For you to pass values from diretivasFilhas you pass the property to the directive you want and it receives this value, eg I'm passing the valor property of PolicyA to the / em>, because I will reuse what the user entered in this field in another section of my form. By doing this I can make any rule without having to create a service itself for this.

Another comment I'm using AngularJs 1.3 and I'm using controllerAs to ensure scope, and instead of using $ scope I do John Papa's approach. (% with%)

/ *    - directiveA - * /

define(['initializer', 'myControllerA'], function (myApp) {
  function myControllerI(ENV) {
    return {
      restrict: 'E',
      templateUrl: ENV.FRONT_END.BASE_URL + '/' + mytemplateA.html,
      controller: 'myControllerA',
      controllerAs: '$ctrl',
      bindToController: true,
      scope: {
        name: '=?',
        zipcode: '=?',
        total: '='
      }
    };
  }

  myApp.directive('directiveA', ['ENV', directiveA]);
});

/ *    - directiveB - * /

define(['initializer', 'myControllerB'], function (myApp) {
  function myControllerB(ENV) {
    return {
      restrict: 'E',
      templateUrl: ENV.FRONT_END.BASE_URL + '/' + mytemplateB.html,
      controller: 'myControllerB',
      controllerAs: '$ctrl',
      bindToController: true,
      scope: {
        car: '=?',
        model: '=?',
        age: '=',
        total: '='  // atributo da minha diretivaA.
      }
    };
  }

  myApp.directive('directiveB', ['ENV', directiveB]);
});

/ *    - Template Father -   * /

<div>
    <directive-a 
       name="$ctrl.name"
       zipcode="$ctrl.zipcode"
       total="$ctrl.total">  
     </directive-a>

     <directive-b 
       car="$ctrl.car"
       model="$ctrl.model"
       age="$ctrl.age"
       total="$ctrl.total" (recebendo propriedade da diretivaA)>  
     </directive-b>
</div>

References:

  • link
  • link
  • link
  • link
  • 13.08.2018 / 19:20