0

I have a directive:

app.directive('eventView', function () {
  return {
    restrict: 'E',
    replace:true,
    templateUrl: 'partials/EventView.html',
    controller: 'eventController',
    link: function(scope, element){  
        scope.$on("$destroy",function() {
            element.remove();
        }); 
    }
  }
});

The controller for the directive:

app.controller('eventController', function($scope, $rootScope){
  $scope.removeEvent = function (){
    $scope.$broadcast("$destroy")
  }
});

There are several instances of the same directives in the rootscope. Each directive is a div in which there is a remove button which is bind to the removeEvent method in the controller definition.

The problem is that when a remove button is pressed in one those div(from eventView directive), all the divs are being removed. I only want the current directive from where the destroy is being broadcasted to be removed. I know its because i'm broadcasting $scope.$broadcast("$destroy") but how can I remove only the current scope, is there a predefined method only for that current scope

3
  • Are these divs rendered thru ng_repeat or do they have some unique identifier?? Commented Jul 13, 2014 at 3:46
  • You should investigate on isolated scopes. Your directives needs to know which scope it's referring to. Here's a good article: sitepoint.com/practical-guide-angularjs-directives Commented Jul 13, 2014 at 4:34
  • @HarishR, its not part of ng_repeat Commented Jul 13, 2014 at 6:43

1 Answer 1

1

if you are using ng-repeat then you can use $index as the unique identifier for those div... just update your code like below:

app.directive('eventView', function () {
  return {
    restrict: 'E',
    replace:true,
    templateUrl: 'partials/EventView.html',
    controller: 'eventController',
    link: function(scope, element){  
        scope.$on("$destroy" + identifier,function() {
            element.remove();
        }); 
    },
    scope: {identifier : '@'}
  }
});

and broadcast the event with identifier

app.controller('eventController', function($scope, $rootScope){
  $scope.removeEvent = function (identifier){
    $scope.$broadcast("$destroy" + identifier)
  }
});

if the divs are not part of ng-repeat, then you will need to have some kind of identifier to identify which div to destroy...

Sign up to request clarification or add additional context in comments.

8 Comments

so, I understand, I should replace scope: {identifier : '@'} where @ with an identifier
whereever you are using this directive you need to pass identifier with it: <event-view identifier="{{$index}}"></event-view>
in the link function, function(scope, element){ scope.$on("$destroy" + identifier,function() { element.remove(); }); I'm getting an error that identifier doesn't exist
identifier is a value which I'll be keeping in a scope variable, the issue is that whenever i update the scope identifier, it is updating in all directives scopes
use isolate scope, like the one in my example.. to keep separate instance for each directive... also you are getting that error because i guess you have not added the isolate scope..
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.