Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AngularJS编码技巧_Javascript_Angularjs - Fatal编程技术网

Javascript AngularJS编码技巧

Javascript AngularJS编码技巧,javascript,angularjs,Javascript,Angularjs,下面是一段代码片段 $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; }

下面是一段代码片段

$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”在init之后被调用了多少次?当年和月同时更改时,是否有任何方法可以调用一次重载数据?像这样:

<input ng-model="year" name="year" id="year">
<input ng-model="month" name="month" id="month">

调用change year=>reloadData时

调用change month=>reloadData时

当change month and year=>reloadData只调用一次时


谢谢

是的,例如,您可以使用多块手表

$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()}
}