Julien DES
Julien DES

Reputation: 113

Communication between directives

my application has 2 directives :

  1. Signup
  2. CreateItem

I want to call Signup from CreateItem if the user is not authenticated but I don't know how to call Signup Directive method from CreateItem Directive method.

This is my code :

.directive('createitem', function($modal, $http, Global) {
return {
restrict: 'E',
transclude: true,
templateUrl : 'views/item/create.html',
link: function(scope, elem, attrs) {
    scope.openCreateItemModal = function() {
    // I want to call openSignupModal if user not authenticated (Global.authenticated)
    }
}}}
.directive('signup', function($modal, $http) {
return {
restrict: 'A',
transclude: true,
templateUrl : 'views/signup/create.html',
link: function(scope, elem, attrs) {   
scope.openSignupModal = function() {

}

}}}; 

This is my view (I want to inject join directive into createitem directive)

<createitem join>

What is the cleanest way to to that?

Thanks.

Upvotes: 0

Views: 34

Answers (1)

charlietfl
charlietfl

Reputation: 171690

Create a simple service factory that contains both methods and inject the factory anywhere you need it in the app

app.factory('MyModals', function() {
  return {
    openCreateItemModal: function(arg1, arg2) {

    },
    openSignupModal: function(arg1, arg2) {

    }
  }

});

.directive('createitem', function($modal, $http, Global, MyModals) {
    return {     
      link: function(scope, elem, attrs) {
        if(! scope.isAuthenticated )
        MyModals.openCreateItemModal(arg1, arg2);
      }
    }
  }

Upvotes: 2

Related Questions