0

我正在创建一个具有国际化的项目,其中标签将位于 /conf/lang/lang_{{lang}}/labels.js 中,它将包含在 index.html 中。lang - app.run() 中的 rootscope 变量设置。索引.html

 <script ng-src="{{labelUrl}}"></script>

app.js - 运行()

$rootScope.$on('$locationChangeStart', function (event, next, current) 
    {

        if($cookieStore.get("config_details") != undefined)
        {
            $rootScope.language = $cookieStore.get("config_details").language;
        }
        else
        {
            $rootScope.language = 'english';
        }
        $rootScope.labelUrl = "conf/lang/lang_"+$rootScope.language+"/labels.js";
})

此脚本文件在 url 更改时正确加载,但手动刷新时 $rootscope 值会破坏,并且脚本文件在加载 html 内容后加载。有人帮我解决这个问题!!!

4

1 回答 1

1

是的,当您尝试重新加载页面时,一切都会被破坏。看这个

    var myApp = angular.module('myApp', ['ngCookies']);

    myApp.controller('MainController', ['$scope', '$rootScope', '$cookies', '$timeout', function($scope, $rootScope, $cookies, $timeout) {
      if ($cookies.get('lang')) {
        $scope.debug = "Browser has got cookies";
        $rootScope.language = $cookies.get('lang');
      } else {
        $scope.debug = "Browser does not have cookies";
        $timeout(function() {
          $cookies.put('lang', 'en');
          $rootScope.language = 'en';
          $scope.debug = "Browser has got cookies";
        }, 2000);
      }
    }]);

例子

尝试角度翻译

于 2016-11-17T14:09:01.660 回答