我正在使用此代码:
$scope.$watch('option.sTest', function (newValue, oldValue) {
if (newValue !== undefined &&
newValue !== null &&
newValue !== oldValue) {
}
});
有了这个会让我的代码看起来很臃肿。有人对我如何以其他方式实现这些检查有任何建议吗?请注意,newValue 或 oldValue 的值 0
是有效的,因此我无法像这样检查:
$scope.$watch('option.sTest', function (newValue, oldValue) {
if (newValue && oldValue &&
newValue !== oldValue) {
}
});
最佳答案
尝试:
$scope.$watch('option.sTest', function (newValue, oldValue) {
if (typeof newValue !== "undefined" && newValue !== null){
}
});
$watch 在 newValue
不等于 oldValue
时触发,因此我们不需要检查 newValue !== oldValue
关于javascript - 有没有更简单的方法可以检查 Angular watch 中的预期更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21641896/