【发布时间】:2014-09-15 15:56:49
【问题描述】:
所以我试图在我的指令中加载模板。该指令是可重用的。但我无法加载模板。我有其他已加载并正常工作的模板。
我得到的错误是:
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>
【问题讨论】:
-
没错,请注意,templateUrl 需要一个来自应用根目录的路径。
-
不确定我是否关注。所以模板在这个项目使用的插件中。
-
是的,请注意 Angular 正在您的 web 应用程序的根目录中搜索 .html 文件。如果您将插件放在某个文件夹中,则需要指定从根目录开始的整个路径。说 /common/directives/someplugin/classificationviewer.html。
-
@KaranShah 您还可以将模板 html 放入与 templateUrl 具有相同 ID 的
ng-template脚本标签中。如果 Angular 在那里找到它,它不会发出 ajax 请求来获取它 -
你解决了这个问题吗?
标签: javascript angularjs templates