3

The title is a bit of a mouthful but there is my current situation:

I have a directive (D1) that is plain markup for a modal popup that I have in the root. I have a directive (D2) that is in a controller's scope (C1). I would like for D2 to set the contents inside of the modal, D1. I have a service (S1) that is injected into C1 that pulls data from the network that I'd like to ultimately inject into D1.

D1 would be as follows (plain markup for a modal): enter image description here

D1 would be as follows after S1 in C1 returns data to D2 to populate D1. D2 would be responsible for defining model data in D1, such as the {{title}}. enter image description here

The idea is to have a generic, decoupled modal (D1) that can be defined/populated through another directive (D2).

I am having a hard time implementing this functionality. Bonus points, I'd like to have D1 to have an API that I could call to populate the modal with different elements such as checkboxes, input, text. This would be different than just building the markup outside of D1 and then completely injecting it into D1.

The code basics are:

myApp.controller('C1', function(S1){
   var results = S1.query();
   // populate D1 attributes with results from S1?  or a getter function in the controller to returns results to D1.
}

myApp.directive('D1', function() {
var createCheckBox = function( name ){ // one of many different element available exposed through D1's API.
    var checkbox = document.createElement('input');
    checkbox.type = 'checkbox';
    checkbox.name = name;
    return checkbox;
}
return{
   restrict: "E",
   templateUrl: '/path/to/my/modal.html',
   controller: function( $scope ){ // hopefully this would be D1's API
       $scope.modalContent = [];
       this.addCheckBox = function( checkboxName ){
           $scope.push( this.createCheckBox( checkboxName ) );
       }
   }
   link: function( scope, element, attrs ){
        // set up events on the modal (such as closing it)
   }
}
} // end of D1

myApp.directive('D2', function() {
    return{
       restrict: "A",
       require: 'D1', // <-- do not think this is possible with my setup.
       link: function( scope, element, attrs ){
            element.bind( 'click', function(){
                // loop through data returned by service
                D1.addCheckBox(/* checkbox's name */ );
                // end loop
            });

       }
    } 
}// end of D2

HTML Markup

<div ng-controller="root">
  <div ng-controller="C1">
     <span D2></span>
  </div>
  <D1></D1>
</div>

Thank you for following this far!

TL;DR: Look for another question to help out with

shaselton
  • 578
  • 4
  • 10
  • 1
    If D1 is responsible for all rendering, does D2 need to be a directive? Can't it be a controller? – Roy Truelove Feb 28 '13 at 20:05
  • I believe D1 is just a modal that can appear outside of or in addition to any page markup. I think the question is how can D2 communicate with D1? – Jonnybojangles Feb 28 '13 at 21:56
  • 1
    Here's my answer to a similar question: http://stackoverflow.com/questions/14883476/angularjs-call-method-in-directive-controller-from-other-controller/14884646#14884646. – satchmorun Mar 01 '13 at 00:47

1 Answers1

0

Wow long question! Not sure I understood it all but following Jonnybojandles comment...

Could you $broadcast and $on?

$rootScope.$broadcast('MyEvent'); // Fire an event

$scope.$on('MyEvent', function () {  });  // Handle the event

Or simply use $rootScope?

Greg
  • 31,180
  • 18
  • 65
  • 85