0
$ stateParams가 필요한 자식 상태에서 일부 변수를 초기화하려고합니다. 나는 다음과 같은 사용 상태를 등록한 것을 들어
:
$stateProvider
.state('parent', {
abstract: true,
url: '/parent',
template: '<div ui-view></div>',
controller: 'ParentCtrl',
}).state('parent.child', {
url: '/:id',
templateUrl: 'template.html',
controller: function ($scope) {
$scope.someFunction();
},
});
이 내 컨트롤러 :
module.controller('ParentCtrl', [
'$scope', '$stateParams',
function ($scope, $stateParams) {
//calling a function in the scope inherited from parent controller
$scope.someFunction = function() {
//$stateParams isn't available.
}
}]);
그러나 된 SomeFuncion은 $ stateParams에서 PARAMS를 호출 할 때 정의되지 않습니다.
는하지만 그 대신 ParentCtrl
에서 함수를 호출의 경우이 같은 상태 컨트롤러 내부에서 실행
$stateProvider
.state('parent', {
abstract: true,
url: '/parent',
template: '<div ui-view></div>',
controller: 'ParentCtrl',
})
.state('parent.child', {
url: '/:id',
templateUrl: 'template.html',
controller: function ($scope, $stateParams) {
//$stateParams is available
},
});
내가 $ stateParams에 PARAMS에 액세스 할 수 있습니다.
누군가 내게 이유를 설명 할 수 있습니까?
@ RadimKöhler 어쩌면 나는 $ stateParams가 $ scope 상속과 어떤 관계가 있는지 놓쳤습니다. 내 의심은 $ stateParams가 params로 채워지기 전에 자식 컨트롤러에 의해 호출 된 함수가 실행된다는 사실과 관련이 있기 때문입니다. 하지만 동일한 하위 컨트롤러 내에서 매개 변수를 사용할 수 있습니다. – belyid
상위 경로에 액세스하려고하십니까? 이 [SO post] (http://stackoverflow.com/questions/28663146/stateparams-in-url-of-abstract-state-u-router-angularjs)에서 설명했듯이 부모 컨트롤러에서 $ stateParams를 얻거나 결의안. 나는 [부모님 컨트롤러를 포함하도록 이것을 확장하는 plunkr] (http://plnkr.co/edit/vqovhyezWkIUMJGHlZ2j?p=preview) - [여기] (http://plnkr.co/edit/vqovhyezWkIUMJGHlZ2j?p=preview), 문제없이 상위 컨트롤러의 stateParams에 액세스 할 수 있습니다. 너는 뭔가를 바라보고있다. – Mahesh