New Router Config dynamic redirectTo

92 views
Skip to first unread message

Greg

unread,
May 21, 2015, 9:04:56 AM5/21/15
to ang...@googlegroups.com
HI

I would go from

app.controller('RouterCtrl', ['$router', RouterCtrl]);

function RouterCtrl($router) {
 $router.config([
  { path: '/', redirectTo: '/page/' + '553e2d15f91e4bd75d000009' }, // SHOULD REDIRECT TO A DYNAMIC HOME PAGE
 
...
}

to (which does not work)

app.controller('RouterCtrl', ['$router', 'SharedData', RouterCtrl]);

function RouterCtrl($router, SharedData) {
 
SharedData.then(function(service){
 $router.config([
 { path: '/', redirectTo: '/page/' + service.homePage }, // SHOULD REDIRECT TO A DYNAMIC HOME PAGE
 
...
}

where SharedData is a promise which return homePage = 553e2d15f91e4bd75d000009

Any help would be appreciated

Regards
Greg

Sander Elias

unread,
May 21, 2015, 2:01:15 PM5/21/15
to ang...@googlegroups.com
Hi Greg,

did you log out your service to check the content of homePage?

Regards
Sander
Message has been deleted
Message has been deleted

Greg

unread,
May 27, 2015, 12:29:55 PM5/27/15
to ang...@googlegroups.com
Hi Sander,

No because it simply does not work. I have no error but there is no redirection and components are not loaded. I was supposing the structure of my code was not good. But if it is form where could the issue comes from?

Regards
Greg

Sander Elias

unread,
May 27, 2015, 10:26:56 PM5/27/15
to ang...@googlegroups.com
Hi Greg,

A few things, the only place where you can use the route like you do it is in the AppController (the root router)
for all other controllers you have to use it like this:

RouterCtrl.$routerconfig = [

 
{ path: '/', redirectTo: '/page/' + '553e2d15f91e4bd75d000009' }, // SHOULD REDIRECT TO A DYNAMIC HOME PAGE

 
];
function RouterCtrl() {
   
...
}


Well, at least for now, it will work like this!

That will restrict what you want to do to the AppController only. For now.At least in the current iteration of the router.
Also to current iteration of the router more or less demands that you name your first controller 'AppController', exactly like that.

Hope this helps you a bit,
Regards
Sander

Greg

unread,
May 28, 2015, 5:09:55 AM5/28/15
to ang...@googlegroups.com
Hi Sander

Not sure to understand. This plunk is working. The new Router RedirectTo page/2 because in service.js I have service.homePage = 2

What I want to do is to use service.js as a promise which return service.home page through SharedData.then(function(service) as on this not working plunk

Such promise are working in http://plnkr.co/edit/ZsFjr3UWnn3DnjD6Ad5O?p=preview like you already explain

Regards
Greg

Sander Elias

unread,
May 28, 2015, 7:25:03 AM5/28/15
to ang...@googlegroups.com
Hi Greg,

your second example works, if you put in a SharedData service, that actually returns a promise.

something like this:

app.service('SharedData', function($q) {
   
var p = $q.defer()
    p
.resolve( { homePage : 2});
   
return p.promise;
})


You can't do a .then on a random object, and expect them the become promises my magic :-)

Regards
Sander


Greg

unread,
May 28, 2015, 10:04:00 AM5/28/15
to ang...@googlegroups.com
Works like a charm :)))))

Thanks Sander
Regards
Greg

PS : Did you have a look at this topic? - I am trying to use $routeParams inside a directive to get template layout depending of the URL
Message has been deleted

Greg

unread,
May 29, 2015, 8:46:17 AM5/29/15
to ang...@googlegroups.com
Hi

Promise seems the most difficult part of Angular :(((

app.factory('SharedHomePageURL', function (SharedData, $q) {
var homePageUrl = SharedData.then(function(service) {
//console.log(service.homePageUrl); //return home
return service.homePageUrl
})
var p = $q.defer()
p.resolve({homePageUrl});
return p.promise
})


return a promise

How can I get the promise value?

SharedHomePageURL.then(function(response) {
console.log(response);

return:

Object {homePageUrl: Promise}
homePageUrl: Promise
$$state: Object
status: 1
value: "home"

and I just need home from value: "home"

Regards
Greg
Reply all
Reply to author
Forward
0 new messages