简体   繁体   English

AngularJS:通过路径参数化控制器

[英]AngularJS: Parameterize controller via route

I use the same controller for multiple views. 我为多个视图使用相同的控制器。 I want to parameterize the controller differently depending on the route taken. 我想根据所采用的路线不同地参数化控制器。

The views display basically the same angular ui grid, hence the same controller. 视图显示基本相同的角度ui网格,因此是相同的控制器。 However, in one view I want to pre-filter the grid for specific data, whereas in the other I don't. 但是,在一个视图中,我想为特定数据预先过滤网格,而在另一个视图中,我不想。

How can I do that? 我怎样才能做到这一点?

app.config(function ($routeProvider) {
    $routeProvider
        .when('/foo',
            {
                controller: 'Ctrl',
                templateUrl: '/foo.html',
            })
        .when('/bar',
            {
                controller: 'Ctrl',
                templateUrl: '/bar.html',
            });
});

app.controller('Ctrl', ['$scope' function ($scope) { .. }]);

Think of it that way. 想一想。 Both routes are the same except one has a filter and one doesn't. 两个路由都是相同的,除了一个有过滤器而一个没有。 so in reality it's the same route with additional parameter filter='some' right so your configuration might be something like this: 所以实际上它是相同的路由与额外的参数filter='some'正确所以你的配置可能是这样的:

app.config(function ($routeProvider) {
$routeProvider
    .when('/foo/:filter?',
        {
            controller: 'Ctrl',
            templateUrl: '/foo.html',
        })
});

and in your controller you'll have $routeParams.filter question mark would be an optional parameter. 在你的控制器中你将有$routeParams.filter问号将是一个可选参数。 Then in the Ctrl you would just look for filter parameter and use filter to render appropriately. 然后在Ctrl您只需查找filter参数并使用filter进行适当的渲染。 Btw your view can stay the same just filter your grid anyways. 顺便说一句,你的视图可以保持不变只是过滤你的网格。 if filter param doesn't exist it will just return same data (unfiltered) 如果过滤器参数不存在,它将只返回相同的数据(未过滤)

Hope that helps. 希望有所帮助。

At the basic level, you could inspect the current route to see what template was being used and branch off that. 在基本级别,您可以检查当前路由以查看正在使用的模板并将其分支。

app.controller('Ctrl', function($route) {
  if ($route.current.templateUrl === '/foo.html') {
    doFoo();
  } else {
    doBar();
  }
});

That would only work if you are using different templates for each route. 只有在为每条路线使用不同的模板时,这才有效。 If you wanted to re-use the same template, the resolve property of the route is very useful. 如果要重新使用相同的模板, 则路由resolve属性非常有用。

app.config(function($routeProvider) {
  $routeProvider.when('/foo', {
    controller: 'Ctrl',
    templateUrl: '/foo.html'
    resolve: {
      whichRoute: function() { return 'foo'; }
    }
  });
});

app.controller('Ctrl', function(whichRoute) {
  if (whichRoute === 'foo') {
    doFoo();
  } else {
    doBar();
  }
});

And even better than that, the resolve properties can accept functions that return values or promises, so you could do the pre-filtering of the data in there. 甚至更好的是, resolve属性可以接受返回值 promise的函数,因此您可以对其中的数据进行预过滤。

app.config(function($routeProvider) {
  $routeProvide.when('/foo', {
    controller: 'Ctrl',
    templateUrl: '/foo.html',
    resolve: {
      dataToDisplay: function(YourDataService) {
        return YourDataService.getSomeData();
      }
    }
  });
});

app.controller('Ctrl', function(dataToDisplay) {
  doTheThing(dataToDisplay);
});

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

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