这是一个代码片段。
$scope.$watch('year', reloadData);
$scope.$watch('month', reloadData);
$scope.year = 2017;
$scope.month = 1;
var reloadData = function() {
/* Reload Data */
}
var init = function() {
$scope.year = 2015;
$scope.month = 3;
}
init();你认为在初始化之后,函数"reloadData“被调用了多少次?有没有年、月同时变更时调用一次reloadData的解决方案?如下所示:
<input ng-model="year" name="year" id="year">
<input ng-model="month" name="month" id="month">调用change year => reloadData时。
调用change month => reloadData时。
当更改月份和年份时,=> reloadData仅调用一次。
谢谢!
发布于 2018-02-21 17:23:42
可以,例如,您可以使用多个手表
$scope.$watchGroup(['year', 'month'], function(newValues, oldValues, scope) {
//newValues[0] is your new year
//newValues[1] is your new month
if((newValues[0] != oldValues[0]) && (newValues[1] != oldValues[1]))
{//reload()}
}https://stackoverflow.com/questions/48902034
复制相似问题