angularjs - Angular js: How to use URL path in a controller to populate scope -


i want able use part of url in controller, in case able set body class based on url, use service fetch other information json file based on url.

if use console.log check what's in $scope.pagedetail.id withing for loop, correct response, returns "undefined" outside of for loop. works on page reload, not when use page's navigation click way through urls.

my controller:

oddproto.controller('urlcontroller', function($scope, $location, pageservice){    // page id url   var pageid  = $location.path().replace("/", "");    $scope.pagedetail = pageservice.query(function(data) {     // correct page details dataset     (var = 0; < data.length; i++) {       if (data[i].id === pageid) {         $scope.pagedetail = data[i];         break;       }     }   });  }) 

my service

oddproto.factory('pageservice', function($resource) {   return $resource('includes/pages.json'); }); 

pages.json

[   {     "id": "blog",     "name": "blogg"   },   {     "id": "cases",     "name": "projekt"   },   {     "id": "contact",     "name": "kontakt"   } ] 

i've tried doing entirely without service, i.e.

$scope.pagedetail = $location.path().replace("/", ""); 

but works on page load well, seems me $location issue here. don't know else can use though.

your controller not recognizing location change. when controller initialized, sets page id, after changing, controller not re-initialized. this, should use

scope.location=$location; scope.$watch('location.path()', function(path) {   $scope.pagedetail = .... }); 

this observe location.path() , if changing, custom magic.

furthermore, suggest make controller directive, dealing styling body-tag.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -