AngularJS - 视图中未更新的变量(不使用范围)

时间:2022-09-15 09:26:47

I've got a problem with angular, A portion of my variable are not updating after changing their values. My header updates just fine my footer only stays with their initial values.

我有角度的问题,我的变量的一部分在更改它们的值后没有更新。我的标题更新很好我的页脚只保留其初始值。

Here are some code :

这是一些代码:

<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>

All my variables are updating fine here except for "language.lblFooter " and "language.lblFooter".

除了“language.lblFooter”和“language.lblFooter”之外,我的所有变量都在这里更新。

here is the declaration of my module

这是我的模块的声明

(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", []);
}());

And here are extracts from the code that updates the variables :

以下是更新变量的代码摘录:

  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));
    }

So every variables are correctly updated except for the on in the footer. Do you guys have any idea why ?

因此除了页脚中的on之外,每个变量都已正确更新。你们有什么想法吗?

I have seen that this problem could be resolved by using $scope.apply() but i'm not using $scope ? I tried this.$digest and this.$apply but neither works.

我已经看到这个问题可以通过使用$ scope.apply()来解决,但我没有使用$ scope?我试过这个。$ digest和这个。$ apply但不起作用。

EDIT: I tried ingecting $scope just for the $scope.$apply(); purpose but it's not working either. I want to precise that I use $http to retrieve the .json file.

编辑:我尝试仅为$ scope指定$ scope。$ apply();目的但它也没有用。我想确切地说我使用$ http来检索.json文件。

Thanks a lot.

非常感谢。

1 个解决方案

#1


1  

You have 2 instances of the controller. When you do ng-change="language.changeLanguage() it only updates that controller.

您有2个控制器实例。当你执行ng-change =“language.changeLanguage()时,它只更新该控制器。

So in the footer there's never called the changeLanguage()

所以在页脚中从来没有调用changeLanguage()

Consider refactoring and using a service or factory for that language change

考虑为该语言更改重构和使用服务或工厂

EDIT
You could also do
<body ng-controller="LanguageController as language">
since you have the controller as it won't affect anything it shouldn't

编辑你也可以做因为你有控制器因为它不会影响任何它不应该

#1


1  

You have 2 instances of the controller. When you do ng-change="language.changeLanguage() it only updates that controller.

您有2个控制器实例。当你执行ng-change =“language.changeLanguage()时,它只更新该控制器。

So in the footer there's never called the changeLanguage()

所以在页脚中从来没有调用changeLanguage()

Consider refactoring and using a service or factory for that language change

考虑为该语言更改重构和使用服务或工厂

EDIT
You could also do
<body ng-controller="LanguageController as language">
since you have the controller as it won't affect anything it shouldn't

编辑你也可以做因为你有控制器因为它不会影响任何它不应该