我认为这基本上只是语法问题,但我设置了这个路由:
app.config(['$stateProvider', function ($stateProvider) {
$stateProvider.state('addAlbum', {
url: "/albums/create/:artistId",
templateUrl: "public/albums/views/album.tpl.html",
controller: "createAlbumController",
data: {
pageTitle: 'Create Album'
},
resolve: {
albums: function (publicArtistServices) {
return publicArtistServices.getAlbums(1);
}
}
});
}]);
我需要getAlbums()方法来使用在URL上传入的artistId:
返回publicArtistServices.getAlbums({需要来自此处的网址的artistId});
我尝试使用 return publicArtistServices.getAlbums($ state.params [0]); 进行猜测但是只是抛出了“$ state is not defined”错误。
这里的语法是什么?
答案 0 :(得分:5)
$stateParams
就是你想要用的。
app.config(['$stateProvider', function ($stateProvider) {
$stateProvider.state('addAlbum', {
url: "/albums/create/:artistId",
templateUrl: "public/albums/views/album.tpl.html",
controller: "createAlbumController",
data: {
pageTitle: 'Create Album'
},
resolve: {
albums: function ($stateParams, publicArtistServices) {
return publicArtistServices.getAlbums($stateParams.artistId);
}
}
});
}]);
答案 1 :(得分:1)
您需要包含$state
参数(将其“注入”到您的函数中):
albums: function ($state, publicArtistServices) {
return publicArtistServices.getAlbums($state.params[0]);
}
如果不起作用,请使用$route.current.params.artistId
,如下所示:
https://stackoverflow.com/a/13433335/584846