Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/366.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.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 如何使用$watch稳定的方式_Javascript_Jquery_Angularjs_Html_Css - Fatal编程技术网

Javascript 如何使用$watch稳定的方式

Javascript 如何使用$watch稳定的方式,javascript,jquery,angularjs,html,css,Javascript,Jquery,Angularjs,Html,Css,我有以下代码 前端控制器 $scope.month=monthNames[$scope.currentmonthnumber]; monthyeartransfer.setvalue($scope.month); $scope.monthchange = function(m) { $scope.month = m; monthyeartransfer.setvalue($scope.month); } $scope.$watch(function(){ $scope.mo

我有以下代码

前端控制器

$scope.month=monthNames[$scope.currentmonthnumber];
monthyeartransfer.setvalue($scope.month);

$scope.monthchange = function(m) {
   $scope.month = m;
   monthyeartransfer.setvalue($scope.month);
}
$scope.$watch(function(){
   $scope.month = monthyeartransfer.getvalue();
}
在上面的控制器中,每次单击,我都会将月值更新为
monthyeartransfer
服务

我用下面的方法得到了这个
profilecontroller

轮廓控制器

$scope.month=monthNames[$scope.currentmonthnumber];
monthyeartransfer.setvalue($scope.month);

$scope.monthchange = function(m) {
   $scope.month = m;
   monthyeartransfer.setvalue($scope.month);
}
$scope.$watch(function(){
   $scope.month = monthyeartransfer.getvalue();
}
但是上面的代码会触发每个动作。但它只需要在服务中的月值发生变化时发生

我尽力了

$scope.$watch('month',function(){
    $scope.month = monthyeartransfer.getvalue();
}

但它不起作用。如何执行此操作???

这是查看函数结果的方法:

$scope.$watch(function () {
   return monthyeartransfer.getvalue();
}, function (val) {
    // Do stuff...
});