0

In my application, a user can choose a subject from a dropdown and create multiple notes about that subject. Each note is a directive which is a child of the subject scope and each note has an isolate scope. At the moment, the user can close a note manually which dirty checks it and allows the user to save or cancel, etc., before the note is removed from the view. What I'd like to achieve, however, is to call the 'closeNote' method on each of the notes when the subject field is changed in the dropdown menu.

I've started to implement this with a directive on the subject dropdown menu which broadcasts an event to the notes on change:

(function(){
'use strict';

 var changeSubjectSelect = function(){

     function linkFn(scope, element, attrs){

         function onSubjectChange(e){

            scope.$broadcast('saveAllNotes', {data: 'some data'});
            scope.$apply(attrs.changeSubjectSelect);

         }

         element.on('change', onSubjectChange)
     }

     return{
         restrict: 'A',
         link: linkFn
     }
 }

angular.module('ganeshaApp')
.directive('changeSubjectSelect', changeSubjectSelect)

})();

Then I'm picking up the event in the each of the note directives:

//in the noteDirective controller

$scope.$on('saveAllNotes', function(event, data){
     console.log(e);
     closeNote();
})

function closeNote(){
     //dirty check the note, call modal to save or cancel, close note 
}

So far the event is being broadcast to each of the isolate child note directives that are opened, but the function closeNote() is only being called by the last directive in the list. Ideally, I'd like the event to be called on each of the directives in sequence, e.g. 'Note X has changes, would you like to save', then 'Note Y has changes, would you like to save', etc. I'm admittedly not very experienced with these methods...it would be great if someone had some tips or advice on how to achieve this type of use case.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
hughesjmh
  • 2,133
  • 2
  • 11
  • 11
  • why all this trouble when you can just bind a value to the directive? – elasticrash Jun 17 '16 at 15:17
  • hmmm...I'm not sure how you mean...it seems I need to access the method closeNote() for each of the directives that are open. Given that they have isolate scopes and the change event is occurring on an element in the parent scope, I thought this would be the best way? – hughesjmh Jun 17 '16 at 15:25
  • well yeah but in both directives and components (i would prefer components for your problem, they provide more straight solution) there is a way to pass a variable. like this – elasticrash Jun 17 '16 at 15:42
  • look this for directives http://stackoverflow.com/questions/16432198/passing-variable-to-angular-directive if you want a component example I can write one – elasticrash Jun 17 '16 at 15:44
  • Oh, I see...I'll give that a shot and see if I can come up with something. Thanks for the advice! – hughesjmh Jun 17 '16 at 15:44

0 Answers0