Javascript 条件ng重复ui路由器
我正在为angular js使用ui路由器($routeprovider和$locationprovider)。我想根据当前的url/路由,在ng repeat中更改要循环通过的数组。我在stack上读过类似的文章,但找不到解决问题的确切方法 这是我的路线Javascript 条件ng重复ui路由器,javascript,angularjs,url,angular-ui-router,angularjs-ng-repeat,Javascript,Angularjs,Url,Angular Ui Router,Angularjs Ng Repeat,我正在为angular js使用ui路由器($routeprovider和$locationprovider)。我想根据当前的url/路由,在ng repeat中更改要循环通过的数组。我在stack上读过类似的文章,但找不到解决问题的确切方法 这是我的路线 .when('/limited', { template: '<di-home></di-home>', activeTab: 'home', case
.when('/limited', {
template: '<di-home></di-home>',
activeTab: 'home',
caseInsensitiveMatch: true,
})
.when('/all', {
template: '<di-home></di-home>',
activeTab: 'home',
caseInsensitiveMatch: true,
})
。当(“/limited”{
模板:“”,
activeTab:'主页',
案例不敏感匹配:正确,
})
。当(“/all”{
模板:“”,
activeTab:'主页',
案例不敏感匹配:正确,
})
当url更改时,我想使用某种if语句将ng repeat从“thingsinctrl.data.all”更改为“thingsinctrl.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>
实现这一目标的最佳方式是什么?你能给我指一下正确的方向/到类似的地方吗?谢谢 在您看来,不要这样做,这是您的控制器的工作。保持视图简单:
<md-card ng-repeat="things in dataToDisplay"></md-card>
PS:别忘了在你的控制器中注入
$location
。我需要注入$location吗?@andrea是的,注入到你的控制器中我无法让它工作。我的html现在看起来和您的示例一模一样,我的js看起来像这样:Controller.$inject=['$scope','update','$document','$location'];/*@ngInject*/function Controller($scope,update,$document,$location){var NewsCtrl=this;if($location.path()=='/home'){NewsCtrl.dataToDisplay=NewsCtrl.data.all;}else if($location.path()=='/limited'){NewsCtrl.dataToDisplay=NewsCtrl.data.limited;}
if($location.path() === '/limited') {
$scope.dataToDisplay = $scope.data.limited;
} else if($location.path() === '/all') {
$scope.dataToDisplay = $scope.data.all;
}