D
D
Deodatuss2015-06-30 14:30:46
Angular
Deodatuss, 2015-06-30 14:30:46

Is it normal practice to communicate directives through a unique event ID?

Let's say that I have a directive that makes it so that when you click on the element to which it is attached, another directive is inserted after it. So, for the first directive, you need an open scope, and for the created one, an isolated one and with a certain action in the second directive, the method of the first one with the data from the second one should be called. Also, everything is complicated by the fact that there can be as many directives of the first type as you like. And so, in what actually and a question. Isn't this approach bad form:

angular.module('myModule')
     .directive('firstType',['$compile',
          function($compile){
          return{
             link:function(scope,elem,attrs){
              elem.on('click',function(){
                 var eventid = Math.random()*1000+new Date().getTime();
                 var second = angular.element('<second-type eventid="'+eventid +'" ></second-type>');
                 angular.element(this).after(second);
                 $compile(second)(scope);
              });
               scope.$on('send'+eventid,function(e,data){
                     alert(data);
               })
          } 
          }
}]) .directive('secondType',[
          function(){
          return{
             scope:{
                    eventid:"@eventid"
             },
             template:'<input  type="text" ng-model="myModel"/><button ng-click="send()">Send</button>',
             link:function(scope,elem,attrs){
                   scope.send = function(){
                         scope.$emit('send'+scope.eventid, scope.myModel);
                   }
             }
}])

Answer the question

In order to leave comments, you need to log in

2 answer(s)
_
_ _, 2015-06-30
@AMar4enko

Is a very bad tone.
Use require in the downstream directive to get a reference to the upstream controller.

N
Nikita Gushchin, 2015-06-30
@iNikNik

This is very bad practice.
You design your directive, define its API...why don't you just pass a callback?
And change the secondType directive accordingly.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question