我正在努力在网址中传递参数。据我所知,使用vm.all属性设置baseFilter url参数设置正确,但每次单击链接都没有任何反应。我还检查了生成的html,我看到了一个ui-sref而不是一个href标签。任何帮助表示赞赏
router.js
(function () {
'use strict';
angular.module('app').config(routing);
/* @ngInject */
function routing($stateProvider, $urlRouterProvider) {
$stateProvider
.state('home', {
url: '/home',
templateUrl: 'home/home.html',
controller: 'HomeContentController as vm'
})
.state('eventsList', {
url: '/eventsList/:baseFilter',
templateUrl: 'events-list/events-list.html',
controller: 'EventsListController as vm'
});
$urlRouterProvider.otherwise('/home');
}
})();
这是我home.html中的标签
<a class="item" ui-sref="eventsList({baseFilter: vm.all})">
HomeController.js
(function () {
'use strict';
angular.module('app').controller('HomeContentController', HomeContentController);
/* @ngInject */
function HomeContentController($scope, $log, $ionicSideMenuDelegate, $ionicPopup, $state, rxEventsService, rxRequests, $cordovaGeolocation) {
// Assign variable `vm` to `this` to reflect that this is a view-model.
var vm = this;
vm.all = "all";
// Bindable Properties
vm.promptUnavailable = promptUnavailable;
// Controller Initialization
function init() {
console.log('init HomeContentController');
}
init();
// Bindable Events
function promptUnavailable(feature) {
$ionicPopup.alert({
title: 'Unavailable Feature',
template: feature + ' is unavailable in this Alpha release'
});
}
}
})();
答案 0 :(得分:2)
如果您设置正确的vm.all
值,您的代码应该可以正常工作。
angular
.module('app', ['ui.router'])
.config(function ($stateProvider, $urlRouterProvider) {
$stateProvider
.state('home', {
url: '/home',
templateUrl: 'home.html',
controller: 'HomeContentController as vm'
})
.state('eventsList', {
url: '/eventsList/:baseFilter',
templateUrl: 'events-list.html',
controller: 'EventsListController as vm'
});
$urlRouterProvider.otherwise('/home');
})
.controller('HomeContentController', function () {
var vm = this;
vm.all = 1;
})
.controller('EventsListController', function ($stateParams) {
var vm = this;
console.log($stateParams);
});
我使用你的代码创建了一个JSBin,它运行正常。如果您看到控制台日志中记录的值,则其值为$stateParams
。 https://jsbin.com/qebimeh/1/edit?html,js,console,output