0

I have angular and Jquery working on my page. How would I go about deleting the image element through the function in my controller? I figured $(this).remove() would handle it, but nothing happens. Any suggestions would be appreciated.

View

   <li class="moment_block" ng-repeat="moment in moments" ng-class="{noDisplay: !moment.screenshotUrl}">
            <div><p>{{moment.title ? moment.title : "No caption"}}</p> <img src="img/filledHeart.png" alt="fave"  ng-click="addToStorage(moment.title, moment.screenshotUrl)"/></div>
            <img src="{{moment.screenshotUrl}}" alt="{{moment.title}}"/>
        </li>

Controller

 $scope.addToStorage = function(title, imgUrl){
    $(this).remove();
    }
2
  • 1
    never do DOM manipulation from a controller, you either want a directive that exists or you want to write your own directive Commented Apr 1, 2014 at 21:57
  • If you do this right using ng-repeat and storing the array of data in your model then you can avoid having 50 img elements you need to maintain and will just have 1 for the whole shebang. Commented Apr 1, 2014 at 22:30

1 Answer 1

1

In this case an ng-if will work. Just create a boolean within your model and in the controller just change the boolean. Within the HTML use ng-if

Something like

HTML

<img src="img/filledHeart.png" ng-if="myModel.someBool" ng-click="dostuff()"/>

JS

$scope.myModel = {someBool = true}
$scope.doStuff = function(){
   $scope.myModel.someBool = false;
}

If you're doing this with repeating elements or something you can just store the bool on the items your repeating over like item.someBool and then just pass item in the function (in this case item is a data element from an array not a DOM element).

If an existing directive doesn't do exactly what you want and you need to do some DOM manipulation on your own you just write up your own directive. The docs on this are very elaborate so it's easy to get lost but you can really just develop them further and better as you come to understand the whole process, this also helps you understand built in directives or third party directives. I put together a plnkr yesterday showing an extremely basic directive for those with no knowledge of them:

http://plnkr.co/edit/N9UiVc0vTdwlo6lBB2xm

Also here's the docs on directives including on creating your own http://docs.angularjs.org/guide/directive

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

3 Comments

Is there a way to trigger that ng-if bool with "this"? I ask this because that img tag is repeated about 30 times. Updating my question with my code.
If you can show this in a plnkr.co even better but there are various solutions when you are iterating... like I say above you can pass the data element to the function and toggle a property of it and just use that for the ng-if. You don't want to run into a scenario where the controller code is in any way aware or tied to the view.
If you do DOM manipulation in the controller it means your controller is dependent on a particular view and can't be re-used (or unit tested in isolation) there's a multitude of problems with doing the manipulation in the controller so best to avoid it. Make a plnkr.co showing an example of what you've got going (can just use Google logo for the image or anything creative commons on the web)

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.