如何根据路由重用与不同的变量angularjs控制器 [英] How to reuse a controller in angularjs with different variables based on route

查看:122
本文介绍了如何根据路由重用与不同的变量angularjs控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要求加载Web服务。我喜欢显示,排序和过滤表的角度的方式,什么我建伟大的工作。现在,我想扩大这个:根据选择的,我想显示不同的数据链路上,但来自同一个Web服务,以同样的方式。我试着不要重复code所以我想重用控制器,现在我很困惑。这意味着,我想

I have a requirement to load a web service. I liked the angular way of displaying, sorting and filtering tables, and what I built worked great. Now I'm trying to expand this: based on the link selected, I'd like to display different data, but from the same web service, in the same way. I'm trying not to duplicate code so I'm trying to reuse the controller and now I'm confused. This means that I want to


  • 显示不同的部分/模板URL。

  • 负载不同的URL参数。

  • JS​​ON已经返回后,映射了一些东西。

什么是解决我所目前考虑的是在配置多变化的最佳方式?我开始通过路由到一个变量(时,('/转换请求/:businessUnit ),然后用它来加载不同的值,但是,我怎么那么变化模板网址是什么?另外,如果我只是添加路由,并加载不同的URL模板(重用控制器),我如何在Ajax调用加载不同的网址?我不是很熟悉的角,所以也许我找错了树...

What would be the best way of tackling what i've so far considered to be more changes in config? I started by routing to a variable (when('/change-requests/:businessUnit'), and then use that to load different values. But, how do I then change the template url? On the other hand, if I just add a route and load a different URL template (reusing the controller), how do I load a different url in the ajax call? I'm not very familiar with Angular, so maybe I'm barking up the wrong tree...

下面是我原来的code的相关部分。还有更多,但这些方法都必须改变。我已经把一些行内注释在那里你会找到位,我想改变,这取决于所选择的链接。

Here are the relevant parts of my original code. There's more to it but none of that has to change. I've put some inline comments where you'll find the bits that I'd like to change, depending on the links selected.

HTML

< D​​IV NG-视图>< / DIV>

路由器:

  $routeProvider.when('/change-requests/business-solutions', {
                    //this template will be different
                    templateUrl: 'js/modules/changeRequests/partials/business-solutions.html',
                    controller: ChangeRequestCtrl
                })

控制器(的部分):

(parts of the) Controller:

        function ChangeRequestCtrl($scope, $location) {

                $scope.loaded = false;
                $.ajax({
                    //this url will be different
                    url: url,
                    success: function(json) {
                        $scope.$apply(function() {
                            $scope.changes = json.map(function(row) {
                                //this array map will be different
                                return row;
                            });
                            $scope.loaded = true;
                        });
                    },
                    error: function() {
                        $scope.error = true;
                    }
                });
        }

如果我没有决策意识,我会尽力澄清。

If I'm not making sense, I'll try to clarify.

推荐答案

如果你想使用一个重新使用控制器的名字,那么你应该使用resolve属性的路线和使用该财产的决心在控制器

If you want to use a reuse controller name then you should be using resolve property with the routes and use that resolve property in the controller

 $routeProvider.when('/change-requests/business-solutions', {
                    //this template will be different
                    templateUrl: 'js/modules/changeRequests/partials/business-solutions.html',
                    controller: ChangeRequestCtrl, resolve: {
            resolvedprop: ['$route', '$q', function ($route, $q) {
               var apiObject = {url: 'abc.com' };                      
               return apiObject     
                     }],
        }
                })


     function ChangeRequestCtrl($scope, $location,resolvedprop) {
       url = resolvedprop.url ;
       }

这篇关于如何根据路由重用与不同的变量angularjs控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆