任何人都可以解释为什么只有下面的第一个 $watch 在同一个 Controller 中触发?两个输入都是分配了每个 ng-model 的文本框。 (提前致谢)
$scope.$watch('search', function() {
if ($scope.watch !== ""){
var filter = "{'name':{'$regex':'(?i).*"+$scope.search+".*'}}";
fetch(filter);
};
});
$scope.$watch('id', function() {
if ($scope.id !== ""){
var filter = "{'id':{'$regex':'(?i).*"+$scope.id+".*'}}";
fetch2(filter);
};
});
最佳答案
尝试这个语法,看看你会在控制台中得到什么
$scope.$watch('id', function(newVal, oldVal) {
console.log(oldVal, newVal);
});
如果日志没问题,为什么不尝试像这样使用newVal
$scope.$watch('id', function(newVal, oldVal) {
if (newVal !== ""){
var filter = "{'id':{'$regex':'(?i).*"+newVal+".*'}}";
fetch2(filter);
};
});
已编辑
关于javascript - 多个 $scope.$watch 在 Angular JS 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44907047/