2

I'm having an issue with scope lost when using $compile to create a dynamic template for my directive. See the code below (trimmed for clarity) :

(function () {
'use strict';

angular.module('cdt.dm.directives').directive('serviceSources', ['$http', '$templateCache', '$compile', '$parse',
function ($http, $templateCache, $compile, $parse) {
    return {
        restrict: 'E',
        replace: true,
        scope: {
            type: '=',
            sources: '='
        },
        link: function (scope, element, attr) {
            var template = 'Template_' + scope.type + '.html';

            $http.get(template, { cache: $templateCache }).success(function (tplContent) {
                element.replaceWith($compile(tplContent)(scope));
            });

            $compile(element.contents())(scope);
        }
    }
}
])
})();

that works and the html template is loaded.

the html template looks like:

<table>
<thead>
    <tr>
        <th>File</th>           
    </tr>
</thead>
<tbody data-ng-reapeat="src in sources">
    <tr>
        <td>{{src.fileName}}</td>
    </tr>
</tbody>

sources is an array with two elements. In the scope of the directive, it is definitely correct, but in the template, the ng-repeat is not working (I guess because sources is undefined at this stage).

Does someone know what I'm doing wrong ?

5
  • 3
    I wonder if you could just use templateUrl instead? Commented Oct 4, 2013 at 13:51
  • No, the template needs to be dynamic, based on the value passed to the 'type' variable on the scope. So I cannot use templateUrl. Commented Oct 4, 2013 at 14:38
  • I've edited the original code as per your recommendation, thanks. Commented Oct 4, 2013 at 16:06
  • 1
    Just to check, it's not the typo in ng-repeat is it? Commented Oct 4, 2013 at 16:19
  • sorry, forgot to tell you to wrap the code inside scope.$apply. Check my updated answer. Commented Oct 6, 2013 at 8:58

1 Answer 1

2

I think there is a typo: ng-repeat instead of data-ng-reapeat, and ng-repeat should be placed on <tr>

<table>
<thead>
    <tr>
        <th>File</th>           
    </tr>
</thead>
<tbody>
    <tr ng-repeat="src in sources">
        <td>{{src.fileName}}</td>
    </tr>
</tbody>

Note that $http.get is asyn and remember to wrap the code inside scope.$apply. You should modify your code like this:

link: function (scope, element, attr) {
            var template = 'Template_' + scope.type + '.html';

            $http.get(template, { cache: $templateCache }).success(function (tplContent) {
              scope.$apply(function(){ 
                element.replaceWith(tplContent);
                $compile(element)(scope);
                //Or
                //element.html(tplContent);
                //$compile(element.contents())(scope);
               });
            });
        }
Sign up to request clarification or add additional context in comments.

5 Comments

ng-repeat and data-ng-repeat are interchangeable. Same goes for all directives, they can all have data prefixed to them, even custom made directives.
@francisco.preller: if you look closely, you will see reapeat instead of repeat
Oh yeah, you're absolutely right. Thought you meant the latter. My bad :)
@francisco.preller: I meant both actually. In the question, I see: the ng-repeat is not working (I guess because sources is undefined at this stage) . I thought the OP was referring to ng-repeat and I also did not know about data-ng-repeat, though. (I usually use ng-repeat this way). Thanks for pointing out data-ng-repeat
Sometimes instead of working late on a Friday evening and posting dumb questions on SO I should just enjoy the weekend.... :) Thanks a lot people for spotting the typo, that was just it... And also thanks for the great insights regarding $http.get, I wasn't finished with that and I'm going to improve it now based on your comment @Khanh TO.

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.