2016-02-18 31 views
1

如果我寫這樣的:

$scope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) { 
    if ($scope.isChanged) { 
     myShowOkCancelDialog(NotifyType.Warn, 'do you want leave?', function (ok) { 
      if (ok) { 
       $state.go(toState, toParams); 
      } 
     }) 
     event.preventDefault(); 
    } 
}); 

將始終要求確定或取消,而我不希望使用$window.confirm

$scope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) { 
    if ($scope.isChanged) { 
     var isjump = confirm('do you want leave?'); 
     if (!isjump) { 
      event.preventDefault(); 
     } 
    } 
}); 
+0

無法驗證,請提供一個小提琴或包含您所使用的特定服務plunker – beaver

回答

0

我想你想要的是重置$scope.isChanged如果用戶想離開。然後當你調用$state.go()條件不會導致警告顯示

$scope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) { 
    if ($scope.isChanged) { 
     myShowOkCancelDialog(NotifyType.Warn, 'do you want leave?', function (ok) { 
      if (ok) { 
       $scope.isChanged = false; 
       // $stateChangeStart will not enter `if` now 
       $state.go(toState, toParams); 
      } 
     }) 
     event.preventDefault(); 
    } 
});