简体   繁体   English

条件ng重复ui路由器

[英]Conditional ng-repeat ui-router

I'm using ui router ($routeprovider and $locationprovider)for angular js. 我正在为角度js使用ui路由器($ routeprovider和$ locationprovider)。 I want to change the array of which to loop through in a ng-repeat depending on the current url/route. 我想根据当前的URL /路由更改在ng-repeat中循环通过的数组。 I have read similar posts here on stack but can't find an exact solution to my problem. 我已经在堆栈上阅读过类似的帖子,但是找不到确切的解决方案。

These are my routes 这些是我的路线

   .when('/limited', {
          template: '<di-home></di-home>',
          activeTab: 'home',
          caseInsensitiveMatch: true,
      })

   .when('/all', {
          template: '<di-home></di-home>',
          activeTab: 'home',
          caseInsensitiveMatch: true,
      })  

when the url changes i want to change the ng-repeat from "things in Ctrl.data.all" to "things in Ctrl.data.limited" with some kind of an if-statement. 当网址更改时,我想使用某种if语句将ng-repeat从“ Ctrl.data.all中的内容”更改为“ Ctrl.data.limited中的内容”。

<md-card class="md-whiteframe-4dp" ng-repeat="things in Ctrl.data.all" flex-xs="100" flex-sm="100" flex-md="45" flex-gt-md="30"></md-card>

What is the best way to accomplish this? 做到这一点的最佳方法是什么? Can you point me in the right direction/ to a similar post? 你能指出我正确的方向吗? Thanks! 谢谢!

Don't do that in your view, it is a job for your controller. 在您看来,不要这样做,这是您的控制器的工作。 Keep the view simple: 保持视图简单:

<md-card ng-repeat="things in dataToDisplay"></md-card>

Then in controller, depending of your URL, set $scope.data : 然后在控制器中,根据您的URL,设置$scope.data

if($location.path() === '/limited') {
    $scope.dataToDisplay = $scope.data.limited;
} else if($location.path() === '/all') {
    $scope.dataToDisplay = $scope.data.all;
}

PS: Don't forget to inject $location in your controller. PS:别忘了在控制器中注入$location

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

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