角UI的路由器将数据传递beetween国去功能不起作用 [英] Angular UI-Router passing data beetween states with go function does not work
问题描述
我刚开始学习angularjs,我采用了棱角分明的UI路由器。我试图从一个状态发送数据到另一个使用 $ state.go
但我没有成功。以下是我迄今为止:
I just started learning angularjs and I am using angular-ui-router. I am trying to send data from one state to another using $state.go
but I have no success. Here is what I have so far:
的我不包括HTML intentionaly,因为我以为,如果需要的话,请告诉我,我会添加它,它是没有必要的。的
我已经配置我的状态,如下:
I have configured my states as below:
$stateProvider
.state('public', {
abstract: true,
templateUrl: 'App/scripts/main/views/PublicContentParent.html'
})
.state('public.login', {
url: '/login',
templateUrl: 'App/scripts/login/views/login.html',
controller: 'loginCtrl'
})
$stateProvider
.state('private', {
abstract: true,
templateUrl: 'App/scripts/main/views/PrivateContentParent.html'
})
.state('private.visits', {
url: '/visits',
views: {
'main': {
controller: 'visitsListCtrl',
templateUrl: 'App/scripts/visits/views/VisitsList.html'
}
}
});
在我的的LoginController
被调用,将执行下面的code:
When my LoginController
is invoked it will execute the below code:
loginModule.controller('loginCtrl', ['$state', function ($scope, $state) {
$state.go('private.visits', { name : "Object"});
}]);
在该 private.visits
页是积极的,我想打印 $ stateParams
:
When the private.visits
page is active, I am trying to print the $stateParams
:
visitsModule.controller('visitsListCtrl', ['$stateParams',
function ($stateParams) {
console.log($stateParams);
}]);
随着事态的状态$ stateParams是一个空的对象。我预期它包含我loginCtrl传递的对象。
As things state $stateParams is an empty object. I expected it to to contain the object I passed in loginCtrl.
修改
看来,如果 private.visits
URL有这个URL格式/访问/:名称
和我还添加属性PARAMS:[名称]我可以访问我从 public.login
状态发送的对象。
的副作用是,所述参数被添加到这是合乎逻辑的URL
It seems that if private.visits
url has this url format '/visits/:name'
and I also add the property params: ["name"] I get access to the object I send from the public.login
state.
The side effect is that the parameters are added to the url which is logical.
我试过没有URL一个孩子的状态做同样的事情,在这种情况下,似乎我没有访问我从 public.login
。
I tried doing the same thing with a child state with no url, and in this case it seems that I do not have access to the params I passed from public.login
.
我如何在孩子的状态发送数据?
How do I send data in child states?
推荐答案
您所要做的就是定义在私有的
的状态,如:名称
参数.visits
What you have to do is to define the name
param in the private.visits
state like:
$stateProvider
.state('public', {
abstract: true,
templateUrl: 'App/scripts/main/views/PublicContentParent.html'
})
.state('public.login', {
url: '/login',
templateUrl: 'App/scripts/login/views/login.html',
controller: 'loginCtrl'
})
$stateProvider
.state('private', {
abstract: true,
templateUrl: 'App/scripts/main/views/PrivateContentParent.html'
})
.state('private.visits', {
// NOTE!
// Previously, we were only passing params as an array
// now we are sending it as an object. If you
// send this as an array it will throw an error
// stating id.match is not a function, so we updated
// this code. For further explanation please visit this
// url http://stackoverflow.com/a/26204095/1132354
params: {'name': null},
url: '/visits',
views: {
'main': {
controller: 'visitsListCtrl',
templateUrl: 'App/scripts/visits/views/VisitsList.html',
resolve: {
name: ['$stateParams', function($stateParams) {
return $stateParams.name;
}]
}
}
}
});
和然后在控制器访问名称
And then in the controller access to the name:
visitsModule.controller('visitsListCtrl', ['name',
function (name) {
console.log(name);
}]);
希望它可以帮助你!
Hope it help you!
这篇关于角UI的路由器将数据传递beetween国去功能不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!