Please tell me about
in angular-ui-route$state.go('xxx',{id:'1'})
Can only this simple string be passed?
If I want to pass object, how should I use global variables?
phpcn_u15822017-05-15 16:54:32
Not only can you pass simple strings, but of course you can also pass objects
The name of the parameter in the router configuration. Take ui-router as an example
$stateProvider.state('state1', {
url: '/path/:id', // 这个地方用简单字符串
templateUrl: '/path/to.html',
params: {
obj: null // 这个地方就可以随便你用了. 因为这个参数没在state的url中体现出来
}
}).
Use $state for page switching
$state.go('state1', {
id: '22',
obj: {
key: 'value'
}
});
Use $stateParams in controller to get parameters
console.log($stateParams.obj)
Of course, if you pass parameters that are not reflected in the URL, the parameters will not be available during operations such as going back.
You can use @whosesmile's method to operate. Use service. LZ can take a look at angular's built-in $cacheFactory
. 使用这个可以生成一个缓存, 执行put get remove removeAll
and other operations
大家讲道理2017-05-15 16:54:32
Usually two options
1. Global variables, that is, using $rootScope
2. Use service. Because service is a single instance and will not be destroyed, you can define a general cache data service to access data. Call service.set(key, value) in the controller that needs to transfer data. When needed Call service.get('key')
伊谢尔伦2017-05-15 16:54:32
I forgot which version ui-router added the following method to maintain the parameters in the url
1.$state
$state.go("state",{datas:{ID:data1,NAME:data2}})
2.Configure router
.state('state',{
url:'path/{datas:json}',
params:{'datas':null}.
...
})
3. Use $stateParam.datas to get