angularjsangularjs-directiveangularjs-templates

Compiling string htmlusing angular compile not working having ng-repeat in it


Sorry for bad question format before. Changing this now

I have a html string which uses the ngrepeat to render the required div..

 var templateHTML = "<div ng-repeat='entity in createCtrl.finalCCList'>\
                         <span>{{entity.name}}</span>\
                     </div>";

Here "createCtrl.finalCCList" has list of entity object which has name and id properties in it. Now when I try to compile this using -

var compiledTemplateHTML = $compile(templateHTML)($scope);
            if (compiledTemplateHTML && compiledTemplateHTML[0]) {
                return compiledTemplateHTML[0].outerHTML;
            }
            else {
                return "";
            }

I get nothing. Whereas i checked and $scope.createCtrl.finalCCList does have the required values.

Am I missing anything here.


Solution

  • Ok. After lot of research I figured issue was the DOM rendered by ng-repeat inside the string html after compiles takes time and I am assigning it before it complete. If i uses the assignment in timeout it works fine.

    So instead of this-

    var compiledTemplateHTML = $compile(templateHTML)($scope);
            if (compiledTemplateHTML && compiledTemplateHTML[0]) {
                return compiledTemplateHTML[0].outerHTML;
            }
            else {
                return "";
            }
    

    I am not returning but assigning to scope in $timeout

    var compiledTemplateHTML = $compile(templateHTML)($scope);
            if (compiledTemplateHTML && compiledTemplateHTML[0]) {
                $timeout(function () {
                    createCtrl.isHeaderContent = compiledTemplateHTML[0].outerHTML;
                }, 0);
            }
    

    Thanks.