2017-09-13 59 views
0

我正在使用MEANstack。這裏是返回空對象的簡單stateparams。代碼:

route.js

var app = angular.module('mean-blog.home', [ 
'ui.router']); 

的app.config(函數($ 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 '' 
[email protected]://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.15/angular-ui-router.js:3140:17 
[email protected]://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 
[email protected]://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

你的狀態被稱爲'viewprofile'而不是'vieworganizer' –

+0

你似乎沒有注入$ stateParams。不應該是.controller('viewProfileCtrl',['$ scope','Users','$ stateParams',函數($ scope,Users,$ stateParams){.... – rrd

回答

0

你沒有state命名vieworganizer,它應該是

<a ui-sref="viewprofile ({organizerid:organizer._id})"> 
+0

謝謝,1個問題已解決另一個是空stateparams ..請你幫我這個請 –

+0
+0

錯誤:無法解析'vieworganizer'從狀態「問題已經解決,因爲狀態名稱是不同的。現在的問題是,我無法獲得控制器中的stateparams值,因爲它返回空對象。 –

0

您需要更新視圖文件

<p> {{organizer._id}}</p> 
    <a ui-sref="viewprofile({organizerid:organizer._id})"> 
     <i class="fa fa-edit"></i>Edit 
</a> 

viewprofile是在app.config定義的狀態url的文件/vieworganizer/:organizerid

+0

解決方案爲空stateparams? –

+0

您是否嘗試過'$ scope.params'而不是'$ stateParams'來獲取參數? – Gaurav