Watch variable and change it

前端 未结 5 1671
广开言路
广开言路 2020-12-24 07:10

In AngularJS I have a directive that watches a scope variable. When the variable contains certain data then I need to alter that variable a bit. The problem is that when I c

5条回答
  •  無奈伤痛
    2020-12-24 07:29

    you can manage it using a bool variable

    $scope.someVarChanged = false;
    
    scope.$watch('someVar', function(newValue, oldValue) {
        console.log(newValue);
        $scope.someVarChanged = !$scope.someVarChanged!;
        if($scope.someVarChanged) {            
            scope.someVar = [Do something with someVar];
        }
    });
    

提交回复
热议问题