简体   繁体   English

如何基于路由在具有不同变量的angularjs中重用控制器

[英]How to reuse a controller in angularjs with different variables based on route

I have a requirement to load a web service. 我需要加载Web服务。 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. 现在,我尝试扩展它:基于所选的链接,我想以相同的方式显示来自同一Web服务的不同数据。 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 这意味着我要

  • display a different partial / template url. 显示不同的部分/模板网址。
  • load different url parameters. 加载不同的网址参数。
  • map a few things after JSON has been returned. 在返回JSON之后映射一些内容。

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... 我首先路由到一个变量( when('/change-requests/:businessUnit' ),然后使用该变量加载不同的值。但是,如何更改模板的url呢;另一方面,如果我只是添加路由并加载不同的URL模板(重用控制器),如何在ajax调用中加载不同的url?我对Angular不太熟悉,所以也许我在错误的树上咆哮...

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: HTML:

<div ng-view></div>

Router: 路由器:

  $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. 如果我没有道理,我会尝试澄清。

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 如果要使用重用控制器名称,则应在路由中使用resolve属性,并在控制器中使用resolve属性

 $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 ;
       }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM