AngularJS / UI Router - Locale in State URL / TemplateURL

AngularJS / UI Router - Locale in State URL / TemplateURL

本文关键字:State URL TemplateURL in UI Router AngularJS Locale      更新时间:2023-09-26

我想要完成的是将$scope变量设置为状态参数,

  .state('form', {
        url: '/' + locale + '/form',
        templateUrl: locale + '/form.html',
        controller: 'formController'
    })

目前我只是使用一个变量

var locale

但是我想让URL动态改变,如果

$scope.locale

更改。例如,如果用户选择英语,则范围将更改为

$scope.locale = 'en';

我希望这也反映在状态/URL中,并将用户带到适当的语言页面。我想我应该这样做:

  .state('form', {
        url: '/' + $scope.locale + '/form',
        templateUrl: $scope.locale + '/form.html',
        controller: 'formController'
    })

是否有办法实时更新$stateParams中的$scope信息??

我怎样才能在templateUrl以及页面的URL中完成实时更改?

您应该通过在url中指定参数来做到这一点,并在templateUrl中注入$stateParams:

  .state('form', {
        url: '/:locale/form',
        templateUrl: function($stateParams) { return $stateParams.locale + '/form.html' },
        controller: 'formController'
    })