Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
103
rated 0 times [  108] [ 5]  / answers: 1 / hits: 105740  / 10 Years ago, mon, august 18, 2014, 12:00:00

I'm trying to implement a language switcher where if a user clicks on de from any given page on an en side - it takes them to that page of the de side. If I console.dir the $state parameter, it exposes the values I'd want with the current property of the given $state. If I try to console.dir the $state.current to focus on the values I want, it only gives the parent state property (my current views are nested).



My current thinking is, I'm on url/en/content, and dynamically I can then have my lang navigation dynamically load the appropriate destination points into some kind of data attribute, pick those up with a custom directive where I'd initiate a go to and set my preferedLanguage value per angular-translate.



The key issue at the moment is exposing that $state name - again, when simply browsing $state the current object gives the values I'd want, but $current.state directly only gives the parent state.



If anyone has a better suggestion of how to do this (in a angular way - no custom cookie junk) I'm happy to take suggestions.



Thanks!



Update! CODE SAMPLES:



Object reference of my states:



var urlStates = {
en: {
home: {
name: 'home',
url: '/en',
templateUrl: 'templates/'+lang+'/home.html',
abstract: 'true'
},
home_highlights: {
name:'home.highlights',
url: '',
templateUrl: 'templates/'+lang+'/home.highlights.html'
},
home_social:
{
name: 'home.social',
url: '/social',
templateUrl: 'templates/'+lang+'/home.social.html'
},
home_map:
{
name: 'home.map',
url: '/map',
templateUrl: 'templates/'+lang+'/home.map.html'
}

};


My States:



$stateProvider
.state(urlStates.en.home)
.state(urlStates.en.home_highlights)
.state(urlStates.en.home_social)
.state(urlStates.en.home_map);

$locationProvider.html5Mode(true);

})


Controller:



.controller('LandingPage', function($translate, $state){
this.state = $state;
this.greeting = Hello;
});


And Lastly, the output I see in the dom:



With this.state = $state;



{
params: {},
current: {
name: home.highlights,
url: ,
templateUrl: templates/en/home.highlights.html },
transition: null
}


With this.state = $state.current



{
name: ,
url: ^,
views: null,
abstract: true
}

More From » angularjs

 Answers
42

this is how I do it



JAVASCRIPT:



var module = angular.module('yourModuleName', ['ui.router']);

module.run( ['$rootScope', '$state', '$stateParams',
function ($rootScope, $state, $stateParams) {
$rootScope.$state = $state;
$rootScope.$stateParams = $stateParams;
}
]);


HTML:



<pre id=uiRouterInfo>
$state = {{$state.current.name}}
$stateParams = {{$stateParams}}
$state full url = {{ $state.$current.url.source }}
</pre>


EXAMPLE



http://plnkr.co/edit/LGMZnj?p=preview


[#69734] Saturday, August 16, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
gregoriocoya

Total Points: 549
Total Questions: 111
Total Answers: 104

Location: Saint Helena
Member since Mon, Jan 16, 2023
1 Year ago
;