角度.js模板在离开并返回路由之前不会渲染
Angular.js templates not rendering until leaving and returning to route
我正在尝试使用$templateCache预加载我的模板。但是,当我的应用中的视图最初加载时,它不会在浏览器中呈现。只有在我第二次返回路线后,模板才会呈现。可能是什么原因造成的?
我正在将我的模板预加载到一个模块中,如下所示:
angular.module("templateCache", []).run(["$templateCache", function($templateCache) {
$templateCache.put("views/view2/view2.html", "<script type='"text/ng-template'" id='"views/view2/view2.html'">'n<p>This is the partial for view 2.</p>'n$templateCache: {{ $templateCache }}'n</script>");
$templateCache.put("views/view1/view1.html", "<script type='"text/ng-template'" id='"views/view1/view1.html'">'n<p>This is the partial for view 1.</p>'n$templateCache: {{ $templateCache }}'n</script>");
}]);
然后,我需要将此模块作为我的应用程序模块中的依赖项,并使用 templateURL 在我的应用程序控制器中使用这些模板,如下所示:
angular.module("myApp", ["ngRoute", "templateCache", "myApp.view1", "myApp.view2"]).config(["$routeProvider", function(e) {
e.otherwise({
redirectTo: "/view1"
})
}]), angular.module("myApp.view1", ["ngRoute"]).config(["$routeProvider", function(e) {
e.when("/view1", {
templateUrl: "views/view1/view1.html",
controller: "View1Ctrl"
})
}]).controller("View1Ctrl", ["$templateCache", function(e) {
console.log(e.get("views/view1/view1.html"))
}]), angular.module("myApp.view2", ["ngRoute"]).config(["$routeProvider", function(e) {
e.when("/view2", {
templateUrl: "views/view2/view2.html",
controller: "View2Ctrl"
})
}]).controller("View2Ctrl", [function() {}]);
请看这个 Plunker 演示我的问题:http://plnkr.co/edit/4rcR8aZQ8fiIRrzsLPiR
不应将这些<script>
标记粘贴到缓存视图中。
$templateCache.put("views/view1/view1.html", "<p>This is the partial for view 1.</p>");
$templateCache.put("views/view2/view2.html", "<p>This is the partial for view 2.</p>");
我用你的 plunker 演示对其进行了测试,似乎工作正常。
相关文章:
- 如何将返回的路由对象串行化并本地存储在传单路由机中
- 如何格式化我返回的url,使其符合我想要的路由
- nodejs或expressjs在没有返回响应的情况下重复调用路由
- 所有可能的路由给定特定的节点,返回到起始节点
- 设置快速路由,找不到API调用返回404
- Ember.js:路由返回多个模型,包括一个promise,给出错误
- 角度.js模板在离开并返回路由之前不会渲染
- 余烬 - 如何将属性从路由文件传递到返回 401 的车把模板
- NodeJs,异步函数在 api 路由返回后完成
- 如何告诉MVC 4视图返回带有子路由的JSON结果
- ajax返回时的角度加载路由
- 角度路由解析返回未定义
- Javascript(Jquery)调用MVC3 Controller/Action/ID的URL路由问题,返回404,
- Node.js AJAX路由在评估引用的函数之前返回数据
- 服务器在ajax调用上返回空响应可能是路由错误
- Backbone.History.扩展({loadUrl: ..})对于有效路由返回false
- js车把根据当前路由返回不同的文本
- Ajax通过javascript路由,控制器应该返回json并返回html页面
- Express js路由返回404
- 在EmberJs中,只有在父路由返回的承诺被解析后,才让子路由触发模型钩子