angularjs - How to bystep this $watch infinite loop?
问题描述
In my angularJs file, I have a watch set on a variable that is set by my md-select
<md-select ng-model="NC.common.commonType" ng-model-options="{trackBy: '$value.id'}" flex="25" placeholder="Common Type" ng-disabled="!NC.isEdit()">
In my controller:
scope.$watch('NC.common.commonType', function (new, old) {
modal.launchModal('Common type changed', '<p>Are you sure?</p>')
.then((result) => {
if (!result) {
vm.module.placementType = angular.copy(oldValue);
});
}, true);
So when they select another option; it triggers a modal. This modal asks you if you are sure you want to change the modal. If you say no, I set the watched variable back to the old value; and ignore the new value. This triggers the $watch infinite loop.
解决方案
这就是 ng-change 的样子
<md-select ng-model="NC.common.commonType" ng-value="NC.common.commonType" ng-change="CC.onChange(NC.common.commonType,'{{NC.common.commonType}}')">
vm.onChange = function(newValue, oldValue) {
modal.launchModal('Placement type changed', '<p>Are you sure?</p>')
.then((result) => {
if (!result) {
vm.module.placementType = angular.copy(oldValue);
}
});
};
这在没有无限循环的情况下有效
推荐阅读
- excel - VSTO Excel 插件,从其他工作簿复制工作表 (VB.NET)
- java - 'startActivityForResult(android.content.Intent, int)' 已弃用图片
- python - Pytorch DataLoader 输出不可迭代
- cassandra - cassandra 如何使用 2 个表获取数据?
- swift - 为什么我的快速收藏视图不显示任何内容
- python - 错误:找不到 pg_config 可执行文件 - 在尝试对 django 应用程序进行 dockerise 时
- java - Cucumber 只运行一个特征文件而忽略其余的
- sql - R : x 比较 (1) 仅适用于原子和列表类型
- javascript - NextJS,导入样式,如果在子文件夹中无法解析
- mysql - 有没有办法在 MySQL 中使用 unix 时间戳按一分钟时间分组?