Friday 29 July 2016

angularjs - Pass `params` for embedded view ui-router



I'm using UI-Router to build an application. I need to have multiple views on one page so I'm using abstract states. I tried to pass the parameter "isEmbedded" to the owner view, but it's unfortunately not working. I'm wondering if its because I'm passing it to a child view. When I console.log($stateParams) in ownerCtrl, it does not show the isEmbedded parameter. Any idea why?



.state('dog', {
url: "",
parent: "dogAbstract",
views: {
"owner": {

templateUrl: 'client/people/views/owner.ng.html',
controller: 'ownerCtrl',
params:{
isEmbedded:true
}
}
}
})



P.S. I got the idea to use params from this question:



Angular ui router passing data between states without URL


Answer



While $stateParams belongs to state, we can use special resolve for a view:



...
views: {
"owner": {
templateUrl: 'client/people/views/owner.ng.html',

controller: 'ownerCtrl',
resolve:{
isEmbedded: function() { return true},
}
}
}


I created an example here with these two child states




.state('parent.child', { 
url: "/child",
templateUrl: 'tpl.html',
controller: 'ChildCtrl',
resolve: {
isEmbedded: function() { return false},
}
})
.state('parent.child2', {
url: "/child2",

templateUrl: 'tpl.html',
controller: 'ChildCtrl',
resolve: {
isEmbedded: function() { return true},
}
})


And controller can consume it:




.controller('ChildCtrl', ['$scope', 'isEmbedded', 
function ($scope, isEmbedded) {
console.log(isEmbedded)
}
])


Check it here


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...