所以我有角度
的这条路线问题是:我使用的api并不总是可以获得音量 有时音量在那里,有时不在 那么有可能我可以使:volume parm是可选的,这样它只会在它存在时显示它并且当没有给它时它会将它删除
图书的音量为book/read/:name/:volume/:chapter/:id
当它没有音量时,它是book/read/:name/:chapter/:id
'use strict';
angular.module('myApp.read', [])
.config( function($stateProvider) {
$stateProvider.state('read', {
url: '/read/:name/:volume?/:chapter/:id',
templateUrl: 'pages/read/read.html',
controller: 'ReadController',
onEnter: function($rootScope){
$rootScope.bodyClass = 'read';
}
});
})
.controller('ReadController', function($scope, $http, $stateParams, $state) {
var name = $stateParams.name; //getting name
var volume = $stateParams.volume; //getting volume
var chapter = $stateParams.chapter; //getting chapter
var id = $stateParams.id; //getting id
$scope.state = $state.current;
$scope.params = $stateParams;
if (volume && volume.length) volume += '/';
$http.get('http://localhost:1337/book/read/' + name + '/' + volume + chapter + '/' + id).success(function(response) {
$scope.read = response;
});
});
答案 0 :(得分:1)
我不确定集合中间是否有一个,但是为了使参数可选,您只需使用?
进行操作,因此您可以:url: '/read/:name/:volume?/:chapter/:id'
当然,您必须更新控制器以处理它不存在。例如:
.controller('ReadController', function($scope, $http, $stateParams, $state) {
var name = $stateParams.name; //getting name
var volume = $stateParams.volume; //getting volume
var chapter = $stateParams.chapter; //getting chapter
var id = $stateParams.id; //getting id
$scope.state = $state.current;
$scope.params = $stateParams;
if (volume && volume.length) volume += '/';
$http.get('http://localhost:1337/book/read/' + name + '/' + volume + chapter + '/' + id).success(function(response) {
$scope.read = response;
});
});