我有角度的问题, 我的变量的一部分在更改其值后未更新。 我的标题更新很好我的页脚只保留其初始值。
以下是一些代码:
<body>
<!-- Navigation -->
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container" ng-controller="LanguageController as language">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<a class="navbar-brand" href="#"> {{ language.lblAppName }}</a>
</div>
<div class="vertical-center" id="language">
<label> {{ language.lblSelectLanguage }} </label>
<select ng-options="item for item in language.languages" ng-model="language.selectedLanguage" ng-change="language.changeLanguage()"></select>
<button ng-click="language.editLanguage()">{{ language.lblEditLanguage }}</button>
</div>
</div>
</nav>
<!-- there will be an ng-route directive here later on -->
<nav class="navbar navbar-inverse navbar-fixed-bottom" role="navigation">
<div class="container-fluid" ng-controller="LanguageController as language">
<div class="navbar-header">
<div class="navbar-brand"> {{ language.lblFooter }} </div>
</div>
<ul class="nav navbar-nav">
<li class="active"><a href="#"> {{ language.lblMainPage }} </a></li>
<li><a href="#">placeholder</a></li>
<li><a href="#">placeholder</a></li>
<li><a href="#">placeholder</a></li>
</ul>
</div>
</nav>
</body>
除了“language.lblFooter”和“language.lblFooter”之外,我的所有变量都在这里更新。
这是我模块的声明
(function(){
// note: when calling angular.module("moduleName,["dependancies"]) a new module is created.
// when callung angular.module("moduleName") the module "modulename" is called.
/** @module
* Creation of the application module: CarViewer */
angular.module("carViewer", []);
}());
以下是更新变量的代码摘录:
var vm = this;
/**
* Initialise the retrieve sequence.
* @function
* @private
*/
function activate() {
// work fine the strings are returned from the backend correctly
return languageService.getLanguages()
.then(onLanguagesComplete, OnError);
}
/**
* Is called when the language retrieve is completed
* assign the different variables
* @function
* @private
* @param {string[]} data - The data returned by the getLanguages() function
*/
function onLanguagesComplete(data) {
vm.languages = data.languages; // list of all available languages
vm.strings = data.strings; // Two dimentional array containing the strings in every language
vm.selectedLanguage = vm.languages[DEFAULT_LANGUAGE];
assignStrings(DEFAULT_LANGUAGE);
};
/**
* Assign the new strings into the different variables
* @function
* @private
*/
function assignStrings(language) {
// Different labels
vm.lblSelectLanguage = vm.strings[language].lblSelectLanguage;
vm.lblEditLanguage = vm.strings[language].lblEditLanguage;
vm.lblAppName = vm.strings[language].lblAppName;
vm.lblMainPage = vm.strings[language].lblMainPage;
vm.lblFooter = vm.strings[language].lblFooter;
}
// and the changeLanguage that is called by the view
/**
* Allow the DOM to changes the language displayed,
* @function
* @public
*/
function changeLanguage(){
assignStrings(vm.languages.indexOf(vm.selectedLanguage));
}
因此除了页脚中的on之外,每个变量都已正确更新。 你们有什么想法吗?
我已经看到使用$ scope.apply()可以解决这个问题,但我没有使用$ scope? 我试过这个。$ digest和this。$ apply但都不起作用。
编辑:我尝试仅为$ scope指定$ scope。$ apply();目的但它也没有用。我想确切地说我使用$ http来检索.json文件。
非常感谢。
答案 0 :(得分:1)
您有2个控制器实例。执行ng-change="language.changeLanguage()
时,它只会更新该控制器。
所以在页脚中,从未调用过changeLanguage()
考虑为该语言更改重构和使用服务或工厂
修改强>
你也可以做
<body ng-controller="LanguageController as language">
既然你有控制器,因为它不会影响任何它不应该