当使用promise时,Angular $ scope变量不会立即更改

时间:2016-04-12 07:11:32

标签: javascript angularjs angular-material

我使用md-switch制作用户界面。

我想要的是

A)。当用户单击md-switch时,更新一些数据。

B)。如果更新失败,md-switch必须返回到原状态。

但在B情况下,md-switch无法正常工作。

奇怪的是当我点击其他区域(按钮或其他东西)时,md-switch才会回来。 (确切地说,$ scope.privacyData仅在此时更改)

任何建议?

这是我的代码。谢谢。

html:

<md-switch ng-show="myVideo" ng-model="privacyData"
ng-change="changePrivacyStatus()" aria-label="Switch 1">
</md-switch>

控制器:

function changePrivacyStatus() {
  // $http.put('/api/lectures', privacyData)
  Promise.reject(new Error('fail'))
  .then(() => {
    console.log('DONE.');
  })
  .catch((e) => {
    $scope.privacyData = !$scope.privacyData;
    console.error(e);
  });
}

0 个答案:

没有答案