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'
}
});
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.
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.
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!
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.
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>
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With