Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I built a directive that dynamically appends html. I used the $compile service to add my ng-click function to the scope so I can remove items from my collection, however, it does not remove it from the DOM. It only removes it from the collection. Any thoughts on how to remove an individual item from the collection and have it update the DOM accordingly?

Here is my code:

app.directive('appendCategory', function(){
 return {
    restrict: 'E',
    controller: function($scope, $http, $compile, $filter){
        $scope.addedCategories = [];
        $scope.categoryList = "";

        $http.get('/api/categories/all-categories').success(function(response){
            $scope.categories = response.map(function(category){
                return {
                    name: category.name
                }
            })
        })

        $scope.onSelect = function ($item) {
            $scope.nameObject = [$item.name];
            $scope.addedCategories.push($scope.nameObject);
        };

        $scope.addCategory = function(){
            var myElement = angular.element(document.querySelector('#appended'));
            $scope.categoryList = ("<span class='label label-default'>" + $scope.selectedCategory + "<i class='fa fa-times' ng-click='removeCategory()'></i></span>");
            var compiled = $compile($scope.categoryList)($scope);
            myElement.append(compiled);

            $scope.addedCategories.push({category: $scope.categoryList && $scope.selectedCategory, deleted:false});
            console.log($scope.addedCategories);
            $scope.selectedCategory = "";
        }

        $scope.removeCategory = function(index){
            $scope.addedCategories.splice(index,1);
            console.log($scope.addedCategories);
        }
    },
    templateUrl: '/category/category.html'
 }
})

Mark-Up: It is appended here.

 <div class="margin-top-1" id="appended"></div>
share|improve this question
2  
Your markup is important. And without exception, $compile should NOT occur in controllers, only in directives. And I suspect that all of the compilation code could be replaced with a proper ng-repeat implementation. – David L Mar 30 at 19:15
    
this is a directive, just not using a link function – Schw2iizer Mar 30 at 19:17
2  
Direct DOM manipulation is rarely a good idea in Angular for exactly this reason: you have to jump through a lot of extra hoops to do what the framework would do for you automatically. If you just ng-repeat over scope.categories you won't have to do all this $compile stuff (and the data binding would remove DOM elements for you when you remove them from the collection.) – Daniel Beck Mar 30 at 19:21
    
Okay that makes a lot of sense. Thanks for your comment. – Schw2iizer Mar 30 at 19:23
1  
@Schw2iizer yes, it is a directive, but you're performing all of this DOM manipulation logic in the controller hook, which is an AngularJS code smell. – David L Mar 30 at 19:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.