首页 > 解决方案 > 更改 ngModel 值以切换选项卡

问题描述

我试图ngModel在单击Cancel按钮(sweet alert)以切换选项卡(divhtml代码中)后更改值,但它不起作用。

这是HTML

<div ng-show="tabsMainMenu=='tabOne'">
   <h1>tabOne</h1>
</div>
<div ng-show="tabsMainMenu=='tabTwo'">
   <h1>tabTwo</h1>
</div>

JS

 swal({
     title: 'Cancel?',
     text: 'Are you sure?',
     icon: 'warning',
     buttons: true
 })
 .then((cancel) => {
   if (cancel) {
   // Go back to Tab One 
   $scope.tabsMainMenu = 'tabOne';
   }
});

只有当我将mouse cursor按钮Cancel放在html页面上时,它才会识别tabsMainMenu ngModel.

有任何想法吗?

标签: javascriptangularjssweetalert

解决方案


Promise 通常是异步的,所以它们在 Angular 的生命周期之外。.then这意味着当你的函数运行时,angular 不知道任何改变。因此,您只需将.then函数内的内容包装在$scope.apply.

https://docs.angularjs.org/api/ng/type/$rootScope.Scope#$apply


推荐阅读