定义路由之前可能填充$ templateCache?路由、定义、templateCache

由网友(守望者°)分享简介:我不希望延迟加载我的路线模板。相反,我希望提前加​​载执行任何路线我所有的航线模板到$ templateCache。I don't wish to lazy load my route templates. Instead I want to load all my route templates into $tem...

我不希望延迟加载我的路线模板。相反,我希望提前加​​载执行任何路线我所有的航线模板到$ templateCache。

I don't wish to lazy load my route templates. Instead I want to load all my route templates into $templateCache ahead of any routes executing.

这是我在做什么:

angular.module('myApp', ['ngRoute']).config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider)
{
    $locationProvider.html5Mode(false);

    $routeProvider.when("/main", {templateUrl: "main", controller: "MainCtrl"})
                  .when("/login",{templateUrl: "login", controller: "LoginCtrl"});
}])
.run(['$location','$templateCache', function ($location, $templateCache)
{
   //...Save templates in $templateCache
   $location.path("/login");
}]);

如果您浏览到 / ,因为它不符合任何路线此工程确定。但是,如果你浏览或 /#/刷新登录似乎航线的尝试之前,我跑块获取运行加载模板,向服务器为它的请求

This works OK if you browse to / because it doesn't match any route. But if you browse or refresh on /#/login it seems the route service attempts to load the template before my run block gets to run and makes a request to the server for it.

反正是有保证code用于填充$ templateCache将在航线的前执行去寻找模板?

Is there anyway to ensure the code that populates the $templateCache will execute before the route service goes looking for the template?

推荐答案

如果您使用咕噜你,我极力推荐的咕噜-角模板为此目的,但如果你不这样做,并希望这样做手动创建中,你也有类似的逻辑,你有什么权利,现在,沿线一些单独的模块:

If you use grunt you I highly recommend grunt-angular-templates for that purpose, but if you don't and want to do this manually create separate module in which you have similar logic to what you have right now, something along the lines:

angular.module('myapp.templates').run(['$templateCache', function($templateCache) {
$templateCache.put(...

,然后简单地引用该模块作为您的应用程序依赖。依赖模块的运行块将首先运行路由逻辑踢之前,这是你想要的。

and then simply reference that module as your app dependency. Run block of dependency module will run first before routing logic kicks in and it's what you want.

阅读全文

相关推荐

最新文章