1

I'm trying to pass an already dynamic value to my directive to fetch a templateUrl for this value. Let me explain with some Sourecode:

<p ng:repeat="cell in field.Cells">
   <cell-element handler="{{cell.handler}}"/>
   <!-- cell.handler is e.g. "User" -->
</p>

myapp.directive('cellElement', function() {
  return {

    restrict: 'E',       

    templateUrl: function (tElement, tAttrs, $compile) {
      return '/ajax/' + tAttrs.handler == undefined ? 'foo' : tAttrs.handler +'.html';
    },       
 }
 });

Unfortunately, the value of tAttrs.handler is always the literal expression "{{cell.handler}}" instead of the respective value. I tried so many different ways - any guess?

UPDATE:

myapp.directive('cellElement', function() {
return {        
   restrict: 'E',
   scope: { handler: '=handler' },
   template: '<ng-include src="\'/ajax/\' + handler"></ng-include>'    
}
}); 

As workaround i used another approach that works. But i would prefer the initial way by using the templateUrl function as i e.g. want to check if "handler" is a valid value.

2
  • Try handler="cell.handler" without curly brackets. Commented May 13, 2014 at 12:59
  • Already tried, does not work. Still the same literal value. Commented May 13, 2014 at 13:00

2 Answers 2

2

I think that this alternative can work:

myapp.directive('cellElement', function() {
  return {
    restrict: 'E',       
    template: '<div data-ng-include="templateUrl"></div>',
    link: function ($scope, iElement, attr) {
        $scope.templateUrl= $scope[attr.handler];
    }

 }

And you have to cosume without backets:

<p ng:repeat="cell in field.Cells">
   <cell-element handler="cell.handler"/>
   <!-- cell.handler is e.g. "User" -->
</p>
Sign up to request clarification or add additional context in comments.

Comments

0

The thing is that probably the template is not compiled, so the value you want is not yet interpolated. If you tried to retrieve it in the link function, it would work.

Comments

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.