1

I am working on an angular application.

In the current build - there is a controller that gets invoked when a user presses a button - it opens up a modal window and populates the contents as such.

I am trying to remove the modal and reconfigure the application, so that it goes to a new page

The first page is a list of people /people When you click a person, it will go to a page like /people/view/34323

I am trying to configure the app stateprovider stack like this:

$stateProvider
.state('people-view', {
    url: "/people/view/:id",
    templateUrl: "partials/people/people-view.html",
    controller: personDialogController,
    resolve: {
        politicservice:function(personservice){
            personservice.getPolitics(id).then = persondata.data;
        }
        biography:function(personservice){
            personservice.getBio(id).then = persondata.data;
        }
        dialogueId:function(personservice){
            //id
        }
    }
})

This will create the basic page...but the services that used to work for this controller now don't.

It's as if the id is not being passed into the resolve functions and it's as if the persondata then, the promise is not calling back?

Blue
  • 22,608
  • 7
  • 62
  • 92
The Old County
  • 89
  • 13
  • 59
  • 129

1 Answers1

1

Pass $stateParams to the resolve function:

$stateProvider
.state('people-view', {
    url: "/people/view/:id",
    templateUrl: "partials/people/people-view.html",
    controller: personDialogController,
    resolve: 
        politicservice:function(personservice, $stateParams){
            return personservice.getPolitics($stateParams.id);
        }
        biography:function(personservice, $stateParams){
            return personservice.getBio($stateParams.id);
        }
        dialogueId:function(personservice, $stateParams){
            //$stateParams.id
        }
})

$stateParams will contain an object of all the parameters as defined in the url string. See this question for more information.

Community
  • 1
  • 1
Blue
  • 22,608
  • 7
  • 62
  • 92
  • Its also written in coffee script. -- still I did try and hard code the id into the service function - and it still didn't take.. maybe it wasn't the correct expected case? – The Old County Jul 26 '16 at 18:43
  • What happens if you just `console.log($state)` from within one of the resolve functions? – Blue Jul 26 '16 at 18:45
  • _ I would have to check it out tomorrow. It was a task I was stuck on at the end of play today. – The Old County Jul 26 '16 at 18:47
  • When I do a console log -- I see the $state -- but when I try to drill down it comes up null -- console.log('state.params', $state.params); console.log('state', $state); console.log('state.params.id', $state.params.id); console.log('state.current', $state.current); – The Old County Jul 27 '16 at 08:59
  • And you're passing `$state` in to the function, correct? – Blue Jul 27 '16 at 09:01
  • And are you using `ng-route` or `ui-router`? – Blue Jul 27 '16 at 09:02
  • $stateParams - returned the id – The Old County Jul 27 '16 at 09:07
  • personservice.getPolitics($stateParams.id).then = persondata.data; -- doesn't seem to work -- the id is going in, I can see the function at that level - but its not coming back with anything - persondata appears empty – The Old County Jul 27 '16 at 09:18
  • PersonService.getPolitics($stateParams.id).then (personRequest) -> personRequest.data console.log('personRequest', personRequest) – The Old County Jul 27 '16 at 09:21
  • See my updated answer. You need to return the promise. – Blue Jul 27 '16 at 09:23
  • but what about the -- then? – The Old County Jul 27 '16 at 09:25
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/118405/discussion-between-frankerz-and-the-old-county). – Blue Jul 27 '16 at 09:28