Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repeated angularjs directive with external template does not render properly when new array items are added.

The following fiddle renders 3 columns of incrementing numbers representing directives with different templates: one inline, one preloaded, and one from an external template. When you select the "add" button the columns increment. The column representing a directive with an external template seems to create a new array when a the add button pushes a new item to the existing array, and then throw the following error:

TypeError: Cannot call method 'insertBefore' of null

Any ideas what is going on?

http://jsfiddle.net/jwanga/EaRHD/

angular.module('app',[]).controller('controller', function($scope){

    $scope.items = [1,2,3,4,5,6,7,8,9];
    $scope.add = function(){

        $scope.items.push($scope.items[$scope.items.length - 1]+1);

    }

}).directive('item1', function(){
    return{
        restrict:'E',
        replace:true,
        scope: {
            data: '=data'
        },
        template:'<li>{{data}}</li>'
    }
}).directive('item2', function(){
    return{
        restrict:'E',
        replace:true,
        scope: {
            data: '=data'
        },
        templateUrl:'item.html'
    }
}).directive('item3', function(){
    return{
        restrict:'E',
        replace:true,
        scope: {
            data: '=data'
        },
        templateUrl:'https://s3.amazonaws.com/thedigitalself-public/jsfiddle-EaRHD-template.html'
    }
});
like image 487
jwanga Avatar asked Feb 10 '13 04:02

jwanga


People also ask

What is ng repeat in AngularJS?

AngularJS ng-repeat Directive The ng-repeat directive repeats a set of HTML, a given number of times. The set of HTML will be repeated once per item in a collection. The collection must be an array or an object. Note: Each instance of the repetition is given its own scope, which consist of the current item.

How directive works in AngularJS?

AngularJS directives are extended HTML attributes with the prefix ng- . The ng-app directive initializes an AngularJS application. The ng-init directive initializes application data. The ng-model directive binds the value of HTML controls (input, select, textarea) to application data.

What is restrict in AngularJS directive?

Restrict. Angular allows us to set a property named restrict on the object we return on our directive definition. We can pass through a string with certain letters letting Angular know how our directive can be used. function MyDirective() { return { restrict: 'E', template: '<div>Hello world!

What is attrs in AngularJS?

Using attrs you are able to access the attributes defined in your html tag like <fm-rating ng-model="$parent.restaurant.price" symbol="$" readonly="true"> So in this case you will have access to the symbol and readonly attributes.


2 Answers

I've solved the issue by wrapping the directive in a parent element and moving the ng-repeat to the parent element. Any insight into why this makes a difference would still be appreciated.

http://jsfiddle.net/jwanga/EaRHD/13/

<li ng-repeat="item in items"><item-3  data="item"></item-3></li>
like image 111
jwanga Avatar answered Oct 07 '22 23:10

jwanga


i got the same message when i was trying to update a div with the html content of a rich text editor. But not as in your case, my ng-repeat was already the parent element!

Well, the problem was caused because the element I needed to work with (inside of the ng-repeat) was not present yet. It was solved with just adding a simple timeout function like this one:

setTimeout(function() {
    $(mySelector).html(htmlContent);
},1);

Hope it helps someone else, cheers!

like image 28
Don Pato Avatar answered Oct 08 '22 00:10

Don Pato