问题描述
我正在寻找一种从捆绑的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 (将#修改为@)