javascript - Angular JS 错误 : $compile:tpload: Failed to load template:

标签 javascript angularjs templates

所以我试图在我的指令中加载模板。该指令是可重用的。但是我无法加载模板。我有其他模板可以加载并正常工作。

我得到的错误是:

GET /ClassificationToolkitForGrails/classificationviewer.html 404 (Not Found) angular.js:8521
Error: [$compile:tpload] Failed to load template: classificationviewer.html

包含指令的 javascript 文件:

/**
 * 
 */
var classificationViewModule = angular.module('ald.classificationview',[]);

classificationViewModule.factory('classificationAPI',function(){
    return {
        getClassification:function($http, artifactId){
            //TODO add URL
            var url = "/Classification/getInfo?artifactId="+artifactId
            return $http({
                method: 'GET',
                url: url
            });
        }
    };
});

/*classificationViewModule.controller('testclassification',['$scope','$http',function($scope,$http){
    $http.get("/Classification/getInfo?artifactId=6450").success(function(data){
        $scope.classification = data;       
    })
}]);*/

classificationViewModule.directive('classificationview', function () {
    return {
        restrict: 'EA',
        scope: {},
        replace: true,
        link: function ($scope, element, attributes) {

        },
        controller: function ($scope, $http, classificationAPI) {

            classificationAPI.getClassification($http).success(function(data,status){               
                //TODO
                $scope.artifactClassification = data;
            }).error(function(data,status){
                if (404==status){
                    alert("no text");
                } else {
                    alert("bad stuff!");
                }
            });            
        },
        //TODO add template url
        templateUrl: "classificationviewer.html"
    };
});

模板文件:

<div>

Hello world

{{artifactClassification}}


</div>

index.html 文件:

<

!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <meta name="layout" content="main"/>
    <title>Classification Toolkit for Grails</title>
    <script type="text/javascript">
        angular.module('classtoolkitApp', [
            'ald.classificationview'
        ]).controller('index', function ($scope) {
        });
    </script>

    <asset:javascript src="ald/classificationview/classificationview.js"/>
</head>
<body >

<div ng-app="classtoolkitApp" ng-controller="index">   

    classification
    <classificationview artifactId=6450/>   

</div>

</body>
</html>

最佳答案

以下是可能的解决方案 1. 检查正确的路径。检查网络选项卡是否获得 200 OK 2. 检查 html 文件的源选项卡 3. 如果您使用的是 webpack,则将 templateUrl 更改为模板并使用 require 加载文件

关于javascript - Angular JS 错误 : $compile:tpload: Failed to load template:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25852115/

相关文章:

c++ - 带有可变参数模板的成员函数指针

c++ - 完美转发 C++ 重载和模板化仿函数及其参数

javascript - NodeJS 未定义的 JSON 值

javascript - 为什么 JSON.parse(JSON.stringify(obj)) 删除 obj 的属性

javascript - 如何在 ng-repeat 中使用anchorScroll() 在索引 x 处启动 View

visual-studio-2010 - 从其他 T4 模板运行 T4 模板

javascript - 使用 jQuery 比较 2 个输入字段的值

javascript - Express.js - foreach 数据数组并将它们存储在数据库中

javascript - 非常基本的示例 - 启用或禁用按钮

javascript - 在 AngularJS 中显示页面底部的最后一条消息不起作用?