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...
});