1

I load a list of recipes before I want to load my page. I need this list in any child state. Depending on the $stateParam I want to pre-filter this list.

Questions

  1. Can I access the $stateParam in any way from the parent state?
  2. Is it a better design to use a service and load the data in the child state and pass it via the service (where are disadvantages)?

I avoided the second solution because the resolve allowed me to switch between the child states without reloading the data (or without checking if the data has been loaded yet).

Research I'm aware that a parent state does not know anything what happens in the child state. But the child state knows of variables in the parent state. I guess this is what this solution used: angular ui-router and accessing child state params from parent

But I didn't get it working and also I don't want to have a url for the parent state because it is never loaded solo.

Code

  .state "widget",
    templateUrl: "app/widget/widget.html"
    controller: "WidgetCtrl"
    resolve:
      Recipes: ($http, $stateParams) ->
        console.log "$stateParams.oldContext #{$stateParams.oldContext}"
        return $http.get('/getProovenRecipes').then( (result) ->
          #console.log result.data
          return result.data
        )
  .state "widget.recipes",
    views:
      'recipes':
        templateUrl: "app/widget/recipes/recipes.html"
        controller: "RecipesCtrl"
        controllerAs: "recipesController"
  .state "widget.recipes.tags",
    url: "/widget/{context}"
    views:
      'tags@widget':
        templateUrl: "app/widget/tags/tags.html"
        controller: "TagCtrl"
        controllerAs: "tagController"
Community
  • 1
  • 1
Andi Giga
  • 3,744
  • 9
  • 38
  • 68
  • How your present code "does not work"? Little more detail there would help in answering.. – mico Jul 07 '15 at 19:09
  • Without using a url in the parent: The console.log returns `undefined` when trying the example in the research link, e.g. `widget/blablub?oldContext=aaaa`. When using a url in the parent state: it just does not work at all and redirects me. Lets say I have not any idea how to access the $stateParam in the `resolve` . – Andi Giga Jul 07 '15 at 19:13
  • just guessing: could you inject $state and access $state.params instead of $stateParams (inspired from http://stackoverflow.com/questions/23081397/ui-router-stateparams-vs-state-params) – mico Jul 07 '15 at 19:45
  • In tried that and the weird thing is that `console.log $state` shows me the params including my param I pass. But as soon as I `console.log $state.params` I get an empty object. – Andi Giga Jul 07 '15 at 19:51
  • Do you mean [Object]? – mico Jul 07 '15 at 20:02
  • No just `{}`and in the other case I see the params and I'm able to expand them. – Andi Giga Jul 07 '15 at 20:12

0 Answers0