Angular $watch | returning the item from function

后端 未结 4 1480
借酒劲吻你
借酒劲吻你 2021-01-13 11:03

I\'m interested to find out why i always have to do this

$scope.$watch( function() {
   return $scope.someData;
}, function( value ) {
   console.log( value         


        
4条回答
  •  猫巷女王i
    2021-01-13 11:17

    I guess it's worth mentioning that passing a function to $watch is useful when you want to monitor a condition:

    $scope.$watch(function() { 
        return $scope.data.length > 0; 
    }, function() {
        // Do something every time $scope.data.length > 0 changes
    });
    

    or

    $scope.$watch(function() { 
        return $scope.prop1 && $scope.prop2;
    }, function() {
        // Do something every time $scope.prop1 && $scope.prop2 changes
    });
    

提交回复
热议问题