2

I try to parameterize the template of a page using a directive. I have an array of objects with a 'type' value. I want to use different templates when types are different.

Here is what I tried:

directive.js

angular.module('core')
  .directive('mySolutionDisplay', function () {
    return {
      restrict: 'E',
      scope: {
        solution: '='
      },
      templateUrl: function (elem, attr) {
        return 'path/to/template/solution-'+attr.type+'.template.html';
      }
    };
  });

view.html

<div class="row">
  <my-solution-display type="vm.solution[0].type" solution="vm.solution"></my-solution-display>
</div>

I get the following error : angular.js:11706 Error: [$compile:tpload] Failed to load template: path/to/template/solution-vm.solution[0].type.template.html

I tried replacing type="vm.solution[0].type" by type="{{vm.solution[0].type}}" but it just added the curly brackets to the error message.

2
  • <div class="row"> <my-solution-display type="{{vm.solution[0].type}}" solution="vm.solution"></my-solution-display> </div> Commented Aug 8, 2016 at 10:42
  • Have you tried like that, i think at type="" it does not resolve the variable name and you can try adding the curly braces there Commented Aug 8, 2016 at 10:43

1 Answer 1

0

Attributes (attrs) has no access to scope variable. They get the dom value after they have been interpolated.

So, use

<my-solution-display type="{{vm.solution[0].type}}" solution="vm.solution"> </my-solution-display>

Note how type="vm.solution[0].type" was changed to type="{{vm.solution[0].type}}"

Sign up to request clarification or add additional context in comments.

2 Comments

I already tried this but it just adds the curly brackets in the error description. angular.js:11706 Error: [$compile:tpload] Failed to load template: path/to/template/solution-{{vm.solution[0].type}}.template.html
Yeah, ignore my answer. Looks like that's just not possible. See the bug: github.com/angular/angular.js/issues/13526. Have a look at: stackoverflow.com/questions/21835471/… for another way of solving this problem.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.