UI路由器onExit转换挂钩

时间:2017-10-06 00:32:37

标签: angular-ui-router

此代码在退出页面时触发两次promptForUnSavedChanges函数。如何使提示只显示一次?

cleanupTransitionHook = $transitions.onExit({}, 
  promptForUnsavedChanges);

function promptForUnsavedChanges() {
    if (ctrl.forms.updateRecipe.$dirty || ctrl.changedPortionCount) {
      if ($window.confirm('You will lose unsaved changes if you leave this page')) {
        if (ctrl.changedPortionCount) {
          ctrl.recipeModel.updateCurrentUserMetaPortionSizeRatio(ctrl.originalScaleFactor, ctrl.userRanges);
        }
        return true;
      }
      return false;
    }
    return true;
  }

1 个答案:

答案 0 :(得分:0)

$ transitions.onExit函数返回是删除器函数。

因此您在返回true之前先调用该函数

ex)cleanupTransitionHook();