0

I was wondering, say I have the following DOM structure in my View / Webpage

<div>
    <p>I am some text or a comment or something</p>
    <a href="" ng-click="deleteThisDiv()">X</a>
</div>
<div>
    <p>I am some text but this Div may have more content like IMG or inputs</p>
    <a href="" ng-click="deleteThisDiv()">X</a>
</div>

I'd like to add a method on the controller called deleteThisDiv that will remove the parent DIV of the href and all the DIV's contents (including the href I just clicked). This is easy with jQuery as I could just get the parent and use $target.remove() however I want to get out of the jQuery way of things and remove the item using AngularJS best practice. I know I could use jqLite as $target.remove() is supported and I guess I could climb the DOM tree to find the DIV but is there a better* way to do this (like using ng-show/ng-hide, etc)? Please note that I can add IDs to my HTML but I don't want to populate the HTML structure with IDs just yet.

  • When I say better I mean an AngularJS way! I just want to get out of the mind set of using jQuery for these things... please note that the HTML is coded and not produced by an array or looping through objects, etc

4 Answers 4

1

I believe the "angular way" would be to have the divs show conditionally using the ng-show directive if what you're looking for is a function that can show/hide the div. If you're looking to literally either have one or the other div in the DOM I'd take a look at ng-if or ng-switch.

Note that ng-if and ng-switch only evaluates once meaning you wouldn't be able to have a function that "removed" a DIV. ng-show on the other hand is evaluated on each $digest cycle

<div ng-show="someExpression>
    <p>I am some text or a comment or something</p>
    <a href="" ng-click="someExpression = !someExpression">X</a>      
    <!--Clicking this will show/hide the div--> 
</div>

<div ng-if="someExpression">
    <p>I am some text but this Div may have more content like IMG or inputs</p>
    <a href="" ng-click="someExpression = !someExpression">X</a>      
    <!--Clicking this has no effect because ng-if is only evaluated once-->
</div>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the advice, I was thinking of having a directive on the DIV that injects a link using transclusion and when I click this link I can delete the DIV removing it from the DOM
0

Use ng-show...

<div ng-show="showFlag">
    <p>I am some text or a comment or something</p>
    <a href="" ng-click="showFlag=false">X</a>
</div>

in your controller you'll need to define the variable like:

$scope.showFlag= true;

Comments

0

You might be looking for this one.

module.controller('TestController',
        function TestController($scope) {

            $scope.deleteThisDiv = function ($event) {
                $event.target.parentNode.parentNode.removeChild($event.target.parentNode);

            };
        });

1 Comment

Technically that still is a form of jQuery: docs.angularjs.org/api/ng/function/angular.element
0

I use this directive for very similar reasons:

<div destroyOnEvent="destroyThisDiv">
    <p>I am some text or a comment or something</p>
    <a href="" ng-click="$emit('destroyThisDiv')">X</a>
</div>

.directive('destroyOnEvent',function($rootScope) {
    return {
        restrict : 'A',
        link : function($scope,$element,$attrs) {   
            $rootScope.$on($attrs.destroyOnEvent,function() {
                $element.remove();
            });
        }
    }
});

Comments

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.