Angular $watch | returning the item from function

后端 未结 4 1484
借酒劲吻你
借酒劲吻你 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条回答
  •  长情又很酷
    2021-01-13 11:12

    With a factory, you need to watch a function because if you pass a string, Angular will evaluate it as an expression against the $scope. Since $data.someData is not defined on your $scope, it won't work.

    To elaborate on @Codezilla's comment, you could assign your factory data to some $scope property, and then watch that:

    $scope.data = $data.someData;
    $scope.$watch('data', function(newValue) { ... });
    

提交回复
热议问题