I am attempting to set the template of a directive dynamically using tips from this link
Here is the code for my directive.
var app = angular.module('sck-table', [
]).directive('tableInput', function(){
return {
restrict: 'E',
scope: {
field:'=field',
},
template:'<ng-include src="getTemplateUrl()"/>',
controller: function($scope){
$scope.count = 0;
$scope.getTemplateUrl = function() {
$scope.count++;
console.log('this has been ran: '+$scope.count);
console.log($scope.field.type);
if($scope.field.type === 'select'){
return 'table/views/table-input-select.html';
}
if($scope.field.type === 'number'){
return 'table/views/table-input-number.html';
}
};
}
};
});
I am using this directive inside of an ng-repeat directive that runs 2 times.(test.fields has a length of 2)
<td ng-repeat="field in test.fields">
<table-input field="field" ></table-input>
</td>
I expect the function getTemplateUrl() to run twice. Once for each item in test.fields.
However when I run this... the console logs 28 times.
I seem to get the desired results... meaning the template is successfully injected into the page, but I was wondering if anyone could tell me why the getTemplateUrl() function is called so many times, and if I can do anything to prevent this.
Thanks in advance.