Exposing the current state name with ui router

前端 未结 6 1553
清歌不尽
清歌不尽 2020-12-14 05:28

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 con

6条回答
  •  别那么骄傲
    2020-12-14 06:12

    Answering your question in this format is quite challenging.

    On the other hand you ask about navigation and then about current $state acting all weird.

    For the first I'd say it's too broad question and for the second I'd say... well, you are doing something wrong or missing the obvious :)

     

    Take the following controller:

    app.controller('MainCtrl', function($scope, $state) {
      $scope.state = $state;
    });
    

    Where app is configured as:

    app.config(function($stateProvider) {
      $stateProvider
        .state('main', {
            url: '/main',
            templateUrl: 'main.html',
            controller: 'MainCtrl'
        })
        .state('main.thiscontent', {
            url: '/thiscontent',
            templateUrl: 'this.html',
            controller: 'ThisCtrl'
        })
        .state('main.thatcontent', {
            url: '/thatcontent',
            templateUrl: 'that.html'
        });
    });
    

    Then simple HTML template having

    {{ state | json }}

    Would "print out" e.g. the following

    { 
      "params": {}, 
      "current": { 
        "url": "/thatcontent", 
        "templateUrl": "that.html", 
        "name": "main.thatcontent" 
      }, 
      "transition": null
    }
    

    I put up a small example showing this, using ui.router and pascalprecht.translate for the menus. I hope you find it useful and figure out what is it you are doing wrong.

    Plunker here http://plnkr.co/edit/XIW4ZE

     

    Screencap


    imgur

提交回复
热议问题