I'm really not quite sure if I don't miss something here, but based on the snippets shown in your question:
PageCtrl
is associated with the state "page.view" and will be executed as many times as the "page.view" state is triggered"page.view"
declared the slug parameter - url: "/pages/:slug",
, which will cause a state change - whenever it changes- If this is true (if I'm not observing something), we can use stateConfig - resolve
- no need to use
$state.params
. We can use $stateParams
(more UI-Router, as I personally said)
Well, if all this is correct, as shown in this working plunger , we can do it like this:
recognizer:
var slugResolver = ['$stateParams', '$http' , function resolveSlug($stateParams, $http){ return $http .get("slugs.json") .then(function(response){ var index = $stateParams.slug; return response.data[index]; }); }];
Adjusted state def:
.state("page.view", { url: "/pages/:slug", views: { "": { controller: "PageCtrl", templateUrl: "page-view.html", resolve: { slug : slugResolver }, }, }, })
And the PageCtrl parameter:
.controller('PageCtrl', function($scope,slug) { $scope.slug = slug; })
Check out everything in action here.
source share