AngularJs模板没有从$templateCache加载

AngularJs templates not loading from $templateCache

本文关键字:templateCache 加载 AngularJs      更新时间:2023-09-26

我目前正在构建一个Angular应用程序,需要通过script标签嵌入到任何其他网页上。

在使用resumeBootStrap初始化应用程序之前,我决定使用一些JS来构建应用程序包装器,ng-view元素等。

然后我使用Grunt构建文件和Grunt -angular-templates来构建一个文件,然后从第三方网页调用。

所以,最初的dom构建等工作得很好,然而,我正试图从$templateCache加载模板来加载我们添加到构建文件中的视图。我可以看到模板被添加到构建文件中,但是,当我尝试加载第三方页面时,我得到错误,因为它试图从本地服务器加载视图,而不是缓存/构建文件。

我的代码如下:

var app = angular.module('MyApp', [
    'ngAnimate',
    'ngCookies',
    'ngResource',
    'ngRoute',
    'ngSanitize',
    'ngTouch',
    'ngScrollbar'
]);
app.config(function ($routeProvider) {
    $routeProvider
        .when('/', {
            templateUrl: 'views/main.html',
            controller: 'MainCtrl'
        })
});
angular.module('MyApp').run(['$templateCache', function($templateCache) {
  'use strict';
  $templateCache.put('views/main.html', '...')
...

任何关于如何做到这一点的建议非常感激,或者实际上,任何关于如何最好地包括我所有的视图等的提示,以便我不需要远程调用主机服务器来获取模板文件。

欢呼

Like:

$templateCache.put('views/main.html', $templateCache.get('views/main.html'))
templateCache美元https://docs.angularjs.org/api/ng/service/

如果你使用grunt

https://github.com/karma-runner/karma-html2js-preprocessor

你是否在html中包含grunt任务生成的js文件,即:

<script src="/templates.js"></script>