How do two commands (not nested in HTML) interact with each other?

im pretty new for angularJS and i have a question (hope this is not so stupid):

I have 2 directives (not nested):

<div directive1></div> <div directive2></div> 

Now I want the directives to talk to each other with the controller (Ive defined "controller:" in directive 1):

 myApp .directive('directive2', function () { return { require: "^directive1", link: function (scope, element, attrs, directive1Ctrl) { directive1Ctrl.doSomething(); } }; }); 

BUT I always get the exception "Controller not found". (I'm not sure if this only caused him to search through his parents).

Is it impossible to use the controller defined in directive1 from directive2 if they are not nested? Should I use a “separate” controller that should work with each other?

+4
source share
2 answers

Do I need to use a “separate” controller that should work with each other?

It's close. The standard way to exchange data and functionality between different parts of your application (two controllers, two directives, a directive and a controller, etc.) is to provide this data or functionality to the service. Then this service can be entered into any component of the application that requires it.

In your case, you can create a service that provides the doSomething function and enter it in both the directive1 and directive2 tags.

+6
source

if you set require: "^directive1", , your directive 2 should be in directive 1:

 <div directive1> <div directive2></div> </div> 

You can also use "? Directive1", which makes it optional.

Simple put: there are two types of controllers: directory controllers and regular ones.

Using controllers, you can set values ​​in the area that will be updated in the view. This is plunkr with one controller and two directives: http://plnkr.co/edit/IicvQfuv8LMOb4iVQen5

 var app = angular.module('plunker', []); app.directive('directive1', function() { return { restrict: 'A', replace: true, template: '<span>{{bar}}</span>' } }); app.directive('directive2', function() { return { restrict: 'A', replace: true, template: '<b>{{bar}}</b>' } }); app.controller('MainCtrl', function($scope) { $scope.name = 'World'; $scope.foo ="layla"; $scope.bar ="hello"; }); 

and html:

 <body ng-controller="MainCtrl"> this is directive1: <div directive1>{{foo}}</div>. <br /> this is directive2: <div directive2>{{foo}}</div>. Hello {{name}}! </body> 

Note that directives are new elements that your browser can parse. They are an HTML extension whose behavior you defined in the app.directive () part.

When AngularJS finds {{foo}} , it will bind it to an area that can be modified using a closer controller that has access to that area. In this case, MainCtrl. You can also put MainCtrl in <div ng-controller="MainCtrl">...</div>

With your code, you will not get a controller because it goes up the tree to find a controller named directive1Ctrl that never happens.

+3
source

Source: https://habr.com/ru/post/1481379/


All Articles