有没有办法在开始时而不是在需要时使 AngularJS 加载部分? [英] Is there a way to make AngularJS load partials in the beginning and not at when needed?
本文介绍了有没有办法在开始时而不是在需要时使 AngularJS 加载部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个这样的路线设置:
I have a route setup like this:
var myApp = angular.module('myApp', []).
config(['$routeProvider', function ($routeProvider) {
$routeProvider.
when('/landing', {
templateUrl: '/landing-partial',
controller: landingController
}).
when('/:wkspId/query', {
templateUrl: '/query-partial',
controller: queryController
}).
otherwise({
redirectTo: '/landing'
});
}]);
我希望能够让 angularjs 在开始时下载两个部分,而不是在请求时下载.
I want to be able to make angularjs download both the partials in the beginning and not when requested.
有可能吗?
推荐答案
是的,至少有 2 个解决方案:
Yes, there are at least 2 solutions for this:
- 使用
script
指令 (http://docs.angularjs.org/api/ng.directive:script) 将你的部分放在最初加载的 HTML 中 - 你也可以填写
$templateCache
(http://docs.angularjs.org/api/ng.$templateCache) 来自 JavaScript,如果需要(可能基于$http
调用的结果)
- Use the
script
directive (http://docs.angularjs.org/api/ng.directive:script) to put your partials in the initially loaded HTML - You could also fill in
$templateCache
(http://docs.angularjs.org/api/ng.$templateCache) from JavaScript if needed (possibly based on result of$http
call)
如果你想使用方法(2)来填充$templateCache
,你可以这样做:
If you would like to use method (2) to fill in $templateCache
you can do it like this:
$templateCache.put('second.html', '<b>Second</b> template');
当然模板内容可以来自 $http
调用:
Of course the templates content could come from a $http
call:
$http.get('third.html', {cache:$templateCache});
这里是那些技术的plunker:http://plnkr.co/edit/J6Y2dc?p=preview
Here is the plunker those techniques: http://plnkr.co/edit/J6Y2dc?p=preview
这篇关于有没有办法在开始时而不是在需要时使 AngularJS 加载部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文