所以我试图在我的指令中加载模板。该指令是可重用的。但是我无法加载模板。我有其他模板可以加载并正常工作。
我得到的错误是:
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/