[英]A parameter of ui-router in controller and resolve
I have configured the following ui-router. 我已经配置了以下ui路由器。
app.config(['$stateProvider', function ($stateProvider) {
$stateProvider
.state('global.editor', {
url: '/posts/editor/{id}',
templateUrl: '/htmls/editor.html',
controller: 'EditorCtrl',
resolve: {
post: ['$stateParams', 'codeService', function ($stateParams, codeService) {
return codeService.getPost($stateParams.id)
}]
}
}
.state('global.new', {
url: '/new',
templateUrl: '/htmls/editor.html',
controller: 'EditorCtrl'
})
.state('global.newTRUE', {
url: '/newTRUE',
templateUrl: '/htmls/editor.html',
controller: 'EditorCtrl'
})
.state('global.editor.panels', {
controller: 'PanelsCtrl',
params: { layout: 'horizontal' },
templateUrl: function (params) { return "/htmls/" + params.layout + '.html' }
}
}])
app.controller('EditorCtrl', ['$scope', '$state', function ($scope, $state) {
$scope.layout = "horizontal";
$scope.$watch('layout', function () {
$state.go('global.editor.panels', { layout: $scope.layout });
});
}]);
As a result, https://localhost:3000/#/new in a browser leads to (the state global.editor
, then to) the state global.editor.panels
. 结果,浏览器中的https:// localhost:3000 /#/ new导致(状态为
global.editor
,然后是)状态global.editor.panels
。
Now, I want to add a parameter connected
: 现在,我想添加一个
connected
参数:
connected
to be false
, and https://localhost:3000/#/newTRUE in a browser makes connected
to be true
connected
为false
,浏览器中的https:// localhost:3000 /#/ newTRUE使得connected
为true
connected
can be past into the controller EditorCtrl
and PanelsCtrl
connected
的控制器EditorCtrl
到控制器的EditorCtrl
和PanelsCtrl
connected
can be available in the resolve
of global.editor
; connected
可以在可用的resolve
的global.editor
; ideally, we could resolve different objects according to the value of connected
. connected
。 Does anyone know how to accomplish this? 有谁知道如何做到这一点?
You can add resolve for new and newTRUE: 您可以为new和newTRUE添加解析:
.state('global.new', {
url: '/new',
templateUrl: '/htmls/editor.html',
resolve: {
isConnected: function() {
return false;
}
},
controller: 'EditorCtrl'
})
.state('global.newTRUE', {
url: '/newTRUE',
templateUrl: '/htmls/editor.html',
resolve: {
isConnected: function() {
return true;
}
},
controller: 'EditorCtrl'
})
And in EditorCtrl (or PanelsCtrl) you can use it like: 在EditorCtrl(或PanelsCtrl)中,您可以像这样使用它:
app.controller('EditorCtrl', ['$scope', '$state', 'isConnected', function($scope, $state, isConnected) {
console.log("connected : " + isConnected); // you can save this value in Service and use it later.
...
}]);
You can use classic approach - in resolve 您可以使用经典方法-解决
Or you can use hidden parameters from angular ui router. 或者,您可以使用来自角度ui路由器的隐藏参数。 Define
params : {isConnected' : null}
in your parent global
state. 在您的父
global
状态下定义params : {isConnected' : null}
。 In: 在:
transition/go
or ui-sref
transition/go
或ui-sref
设置参数 definition is like this: 定义是这样的:
$stateProvider
.state('global.newTRUE', {
url : '/:newTRUE',
params : {
'isConnected' : false
}
});
}
and in controller you get in from $stateParams
. 在控制器中,您可以从
$stateParams
获得。 Problem with this approach is hidden parameters are loses in refresh page, except if is set default value 这种方法的问题是刷新页面中的隐藏参数丢失,除非设置了默认值
You can surely use the params
of UI-Router states' config to not show it in URL and achieve all mentioned points. 你肯定可以使用
params
UI-路由器状态的配置,以在URL没有表现出来,并实现所有提到的观点。
Also, as per #2 , you need connected
to be false
for /new
and true
for /newTRUE
. 此外,按照#2的要求 ,对于
/new
, connected
必须为false
;对于/newTRUE
, connected
必须为true
。 We can do so by passing true
or false
as default value for those states. 我们可以通过传递
true
或false
作为这些状态的默认值来实现。 Something like this: 像这样:
$stateProvider
.state('global.editor', {
url: '/posts/editor/{id}',
templateUrl: '/htmls/editor.html',
params: { connected: null },
controller: 'EditorCtrl',
resolve: {
post: ['$stateParams', 'codeService', function ($stateParams, codeService) {
return codeService.getPost($stateParams.id)
}]
}
}
.state('global.new', {
url: '/new',
templateUrl: '/htmls/editor.html',
params: { connected: false }, // default false for /new
controller: 'EditorCtrl'
})
.state('global.newTRUE', {
url: '/newTRUE',
templateUrl: '/htmls/editor.html',
params: { connected: true }, // default true for /newTRUE
controller: 'EditorCtrl'
})
.state('global.editor.panels', {
controller: 'PanelsCtrl',
params: { layout: 'horizontal', connected: null },
templateUrl: function (params) { return "/htmls/" + params.layout + '.html' }
}
For #3 , In order to access connected
in your controllers ( EditorCtrl
and PanelsCtrl
) you can inject $stateParams
to controller and use $stateParams.connected
to get it. 对于#3,为了访问
connected
在你的控制器( EditorCtrl
和PanelsCtrl
)你可以注入$stateParams
到控制器,并使用$stateParams.connected
得到它。
For #4 , ( This is more or less similar to achieveing #3 ) 对于#4 ,( 或多或少与实现#3相似 )
Just like you get $stateParams.id
, you can have $stateParams.connected
as well, which you can use to resolve different objects according to the value of connected
. 就像你得到
$stateParams.id
,你可以有$stateParams.connected
还有,你可以用它根据价值来解决不同的对象connected
。 Something like this: 像这样:
.state('global.editor', {
url: '/posts/editor/{id}',
templateUrl: '/htmls/editor.html',
params: { connected: null },
controller: 'EditorCtrl',
resolve: {
post: ['$stateParams', 'codeService', function ($stateParams, codeService) {
return $stateParams.connected ?
codeService.getPost($stateParams.id) :
codeService.getSomethingElse($stateParams.id)
}]
}
}
But, for that to work, make sure that you are passing connected
as params when you visit global.editor
state (using $state.go
or ui-sref
) 但是,要使其正常工作,请确保在访问
global.editor
state(使用$state.go
或ui-sref
)时, global.editor
connected
传递为参数。
Hope this helps! 希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.