ng-repeat with ng-include not working

前端 未结 3 831
梦毁少年i
梦毁少年i 2021-02-14 19:07

I am trying to use an ng-repeat that includes an ng-include. The problem is that the first element in the ng-repeat is just the ng-include

3条回答
  •  渐次进展
    2021-02-14 19:29

    I ran into the same problem, and finally figured out that the first element has not been fetched and compiled in time for the first ng-repeat iteration. Using $templateCache will fix the problem.


    You can cache your template in a script tag:

    
    


    Or in your app's run function:

    angular.module("app").run(function($http, $templateCache) {
        $http.get("/views/template.html", { cache: $templateCache });
    });
    


    You can also use $templateCache inside your directive, although it's a bit harder to setup. If your templates are dynamic, I would recommend creating a template cache service. This SO question has some good examples of template caching inside a directive and a service:

    Using $http and $templateCache from within a directive doesn't return results

提交回复
热议问题