javascript - distinguish scope method of custom directives angularjs -


So I have that you can see, you can see that I scope.click The methods I use on Link has an NG-click function on it and I have defined the scope.click = function () {} inside the 'myDirective' custom command.

The question is, when I click on the link, why it always refers to the second element of the custom instruction, why can not it mention both elements? How can I get it and how to separate both the elements inside the 'myDirective' Custom Directive within the scope.click function?

The problem is that you do not have any separate scope in your instructions. This means that both instructions share the scope with the controller. As you are using your instructions twice, you define the scope.click event twice, so you overwrite the scope.click of the first element Are there. In addition to this you can & lt; A & gt; overwrite the ng-click of the tag.

  var app = angular. Module ('myApp', []); App.controller ('MainCtrl', function ($ radius) {$ scope.click = function () {console.log ("Inside Controller");}}); Dipa Neg-click = 'click ()' & gt; Me & lt; / Div & gt; Click; 'My directive', function () {return: restricted: 'EA', radius: {}, template: link: function (scope, elm, ether) {scope.click = function () {console.log (" Inside Directive "+ attr ['id']);}}}})  
  & lt; Script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min .js" & gt; & Lt; / Script & gt; & Lt; Div ng-app = "myApp" ng-controller = "mancruten" ng-init = "test = 1"; & Gt; & Lt; A href = 'javascript:;' Ng-click = "click ()" & gt; Click the link & lt; / A & gt; & Lt; My-directive id = "elm" & gt; & Lt; / My-directions & gt; & Lt; My-directive id = "elm2" & gt; & Lt; / My-directions & gt; & Lt; / Div & gt;  

In this example, I define a separate scope with the scope: {} . Now each director has its own scope. In addition, I define click () for each region.

UPDATE

If you really want to execute a function, you can do it this way when clicking on an element outside the Directorate . However, this is not a good solution I would usually think of solving it otherwise than my problem.

  var app = angular. Module ('myApp', []); App.controller ('MainCtrl', function ($ radius) {$ scope.click = function ($ event) {$ scope.activateElement [$ event.target.id] ();} $ scope.activateElement = Array (); }); App.directive ('myDirective', function () {return: {ECA, link: function (scope, elm, etr) {scope.activateElement [attr ['forid']] = function () {console.log} (" Inside director "+ attr ['forid']);}}}});  
  and lieutenant; Script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js" & gt; & Lt; / Script & gt; & Lt; Div ng-app = "myApp" ng-controller = "main control" & gt; & Lt; A href = 'javascript:;' Ng-click = "click ($ event)" id = 'elm' & gt; Click the link & lt; / A & gt; & Lt ;! - When I click on the link above, then I can say 'Elim' Custom Direction - & gt; & Lt; My-directive forid = "elm" & gt; & Lt; / My-directions & gt; & Lt; A href = 'javascript:;' Ng-click = "click ($ event)" id = 'elm2' & gt; Click the link & lt; / A & gt; & Lt ;! - When I click above the link, then I am 'Elim 2' custom directive - & gt; & Lt; My-directive forid = "elm2" & gt; & Lt; / My-directions & gt; & Lt; / Div & gt;  


Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -