javascript - 更改 ngModel 值以切换选项卡
问题描述
我试图ngModel
在单击Cancel
按钮(sweet alert
)以切换选项卡(div
在html
代码中)后更改值,但它不起作用。
这是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
.
有任何想法吗?
解决方案
Promise 通常是异步的,所以它们在 Angular 的生命周期之外。.then
这意味着当你的函数运行时,angular 不知道任何改变。因此,您只需将.then
函数内的内容包装在$scope.apply
.
https://docs.angularjs.org/api/ng/type/$rootScope.Scope#$apply
推荐阅读
- reactjs - 数据库没有保存在部署在 Heroku 上的 Django 应用程序中
- python - 从 Windows 批处理文件运行 python 脚本 - 文件名冲突
- python - pandas get first element of contigous values to perform sessionization i.e. obtain the sesion start event
- javascript - 我有一个显示股票 png 图像的 URI,我想在我的 javascript 平面列表中附加股票代码?
- python - 如何按给定日期范围的python时间戳过滤GCP stackdriver日志
- python - 使用 pandas 正则表达式基于逗号字符分隔列数据
- c++ - 使用 cpp 将日期字符串解析为 int
- python - 在 Python 中通过 TCP 套接字发送列表
- c# - 在 xamarin 表单中的 CollectionView 的一行中收集一个 Button 的标识符
- asp.net - 如何屏蔽来自 siteA.com 的 url 以从 siteB.com/page1.aspx?id=1234 检索数据 - ( IIS - URLrewrite - ARR)