热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何在控制器内手动注入路由解析数据?-Howcanyoumanuallyinjectrouteresolvedatainsideacontroller?

Ihave2routesthatshareacontroller,oneneedsdataresolvedpriortotheviewloading,andthe

I have 2 routes that share a controller, one needs data resolved prior to the view loading, and the other does not need the resolved data.

我有两条共享一个控制器的路由,一个需要在视图加载之前解析数据,另一个不需要解析数据。

Routing segment example:

路由段示例:

...
when('/users', {
    controller: 'UsersCtrl',
    templateUrl: '/partials/users/view.html',
    resolve: {
        resolvedData : ['Accounts', function(Accounts) {
            return Accounts.get();
        }]
    }
}).
when('/users/add', {
    controller: 'UsersCtrl',
    templateUrl: '/partials/users/add.html'
})
...

Controller example:

控制器示例:

app.controller('UsersCtrl', ['$scope', 'Helper', 'resolvedData', 
    function($scope, Helper, resolvedData) {
        // this works for the first route, but fails for the second route with
        // unknown "resolvedDataProvider"
        console.log(resolvedData); 
}]);

Is there any way I can get the resolvedData in the controller without explicitly using the resolve name as a dependency? So a check can be performed?

有没有什么方法可以在控制器中获取resolvedData而不显式使用解析名称作为依赖项?那么可以进行检查吗?

Using the $injector does not work. I would like to do something similar to:

使用$ injector不起作用。我想做类似的事情:

if ($injector.has('resolveData')) { 
     var resolveData = $injector.get('resolveData');
}

However this does not work even for the route that has the resolveData set ('/users'):

但是,即使对于具有resolveData set('/ users')的路由,这也不起作用:

app.controller('UsersCtrl', ['$scope', 'Helper', '$injector', 
    function($scope, Helper, $injector) {
        // this does not work -> fails with the unknown "resolvedDataProvider" as well
        $injector.get('resolvedData');
}]);

Can this be done in angularjs? Or should I just create a new controller?

这可以在angularjs中完成吗?或者我应该创建一个新的控制器?

Thank you.

谢谢。

2 个解决方案

#1


7  

Looks like I figured out another way to go. The resolved data is part of the $route. So you can access it using:

看起来我想出了另一种方法。已解析的数据是$ route的一部分。所以你可以使用:

app.controller('UsersCtrl', ['$scope', '$route', 'Helper', 
    function($scope, $route, Helper) {

        if ($route.current.locals.resolvedData) {
            var resolvedData = $route.current.locals.resolvedData;
        }
}]);

#2


5  

If the other route doesn't need it, just inject undefined on that route:

如果其他路由不需要它,只需在该路由上注入undefined:

router:

路由器:

when('/users', {
    controller: 'UsersCtrl',
    templateUrl: '/partials/users/view.html',
    resolve: {
        resolvedData : ['Accounts', function(Accounts) {
            return Accounts.get();
        }]
    }
}).
when('/users/add', {
    controller: 'UsersCtrl',
    templateUrl: '/partials/users/add.html',
    resolve: {
       resolvedData: function() {
          return undefined;
       }
    }
})

controller:

控制器:

app.controller('UsersCtrl', ['$scope', 'Helper', 'resolvedData', 
    function($scope, Helper, resolvedData) {
        if(resolvedData){
          //set some scope stuff for it
        } else {
         //do what you do when there is no resolvedData
        }
}]);

推荐阅读
author-avatar
袁韦伦世彦琬育
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有