我正在使用MEANstack。这是返回空对象的简单stateparams。代码:
route.js
var app = angular.module('mean-blog.home', [
'ui.router']);
app.config(function($ stateProvider,$ urlRouterProvider){ $ stateProvider
.state('organizers', {
url: '/organizers',
templateUrl: '/home/templates/organizers.html',
controller: 'OrganizerCtrl'
})
.state('viewprofile', {
url: '/vieworganizer/:organizerid',
templateUrl: '/home/templates/viewprofile.html',
controller: 'viewProfileCtrl'
})
$urlRouterProvider.otherwise("/");
});
controller.js
.controller('viewProfileCtrl', function($scope, Users, $stateParams) {
console.log($stateParams) //returns empty object
})
查看档案
<p> {{organizer._id}}</p>
<a ui-sref="vieworganizer({organizerid:organizer._id})">
<i class="fa fa-edit"></i>Edit
</a>
这个ui-sref也给了我以下错误。
Error: Could not resolve 'vieworganizer' from state ''
transitionTo@https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.15/angular-ui-router.js:3140:17
go@https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.15/angular-ui-router.js:3068:14
link/</transition<@https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.15/angular-ui-router.js:4181:13
f/l<@https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:138:473
e@https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:40:72
nf/n.defer/c<@https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:44:155
如果我打开直接网址
http://localhost:3000/vieworganizer/59b8c14ebed9a011bc5acb54
它返回空的stateparams对象。
任何可以帮助我解决此问题的人!
答案 0 :(得分:0)
您没有名为 state
的 vieworganizer
,它应该是
<a ui-sref="viewprofile ({organizerid:organizer._id})">
答案 1 :(得分:0)
您需要更新视图文件
<p> {{organizer._id}}</p>
<a ui-sref="viewprofile({organizerid:organizer._id})">
<i class="fa fa-edit"></i>Edit
</a>
viewprofile
是在app.config
文件中为网址/vieworganizer/:organizerid