search

Home  >  Q&A  >  body text

javascript - The $rootScope global variable has been updated in the controller of the data entry page, but the variable is still the old value when accessed during beforeLeave?

Code in the controller of the data entry page:

.controller("MobileInputCtrl", function ($scope, $rootScope) {
  $scope.$watch("myForm.$dirty", function (newVal) {
    $rootScope.isDirty = newVal;
  });
})

A test<p> is placed on the data entry page to display the value of the $rootScope.isDirty global variable to prove that the variable value has been correctly updated to true.

$ionicView.beforeLeave() event handling method code registered in the top-level module:

angular.module("app", [])

.run(function ($ionicPlatform, $rootScope) {
  $ionicPlatform.ready(function () {
    $rootScope.$on("$ionicView.beforeEnter", function (event, view) {
      $rootScope.isDirty = false;
    });

    $rootScope.$on("$ionicView.beforeLeave", function (event, view) {
      alert($rootScope.isDirty); // 显示仍然为false,百思不得其解!
    });
  });
});

In the test of the data entry page, it has been clearly seen that isDirty has been correctly updated to true. But when I left the data entry page, I saw that the isDirty value displayed in the pop-up prompt box was still false. I was puzzled! Could it be that this variable can have two values? ? ?

阿神阿神2733 days ago599

reply all(1)I'll reply

  • 巴扎黑

    巴扎黑2017-05-31 10:43:00

    It’s best not to use ionic, caching is a big pitfall. . .

    reply
    0
  • Cancelreply