如何解决为什么我的AngularJS模板加载器只能用于1.6.1版,而不能用于1.7版或1.8版?
我正在寻找一种从捆绑的html文件加载ng-templates的方法,以用作ng-include中的src。
我找到了一个完全适合我的演示,并且在AngularJS 1.6.1中运行良好。不幸的是,在1.7和更高的新版本中,它不再起作用。
var app = angular.module("test",[]);
app.factory("$templateCache",function($cacheFactory,$http,$injector) {
var cache = $cacheFactory("templates");
var allTplPromise;
return {
get: function(url) {
var fromCache = cache.get(url);
if (fromCache) {
return fromCache;
}
if (!allTplPromise) {
allTplPromise = $http
.get("all-templates.html")
.then(function(response) {
$injector.get("$compile")(response.data);
console.log(response.data);
return response;
});
}
return allTplPromise.then(function(response) {
var result = {
status: response.status,data: cache.get(url),headers: response.headers
};
return result;
});
},put: function(key,value) {
cache.put(key,value);
}
};
});
<!DOCTYPE html>
<html ng-app="test">
<head>
<title>Loading all templates in one file...</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.js"></script>
</head>
<body>
<div ng-include="'/one'">Not working...</div>
<div ng-include="'/two'">Not working...</div>
</body>
</html>
- 1.7版中发生了哪些更改而导致了问题?
- 我该如何解决?
- 还有更好的选择吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。