我正在创建一个具有国际化的项目,其中标签将位于/conf/lang/lang_{{lang}}/labels.js中,该项目将包含在index.html中。 app.run()中的lang - rootscope变量设置。 Index.html
<script ng-src="{{labelUrl}}"></script>
app.js - run()
$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内容后加载脚本文件。 有人帮我解决这个问题!!!
答案 0 :(得分: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);
}
}]);