2017-09-02 26 views
1

我有这样的代码:JQuery的动画和AngularJS

HTML代码:

<!DOCTYPE html> 
<html ng-app="test"> 

    <head lang="en"> 
    <meta charset="utf-8"> 
    <title>Wtf is that</title> 
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.6.5/angular.min.js"></script> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
    <link rel="stylesheet" href="style.css"> 
    <script src="script.js"></script> 
    </head> 

    <body ng-controller="testCtrl"> 

    {{test}} 
    <br /> 
    <button ng-click="set()">set</button> 
    <button ng-click="setTimeout()">setTimeout</button> 

    </body> 

</html> 

AngularJS代码:

var app = angular.module('test', []); 

app.controller('testCtrl', function($scope, $timeout) { 
    $scope.test = false; 
    $scope.set = function() { 
     $timeout(function() { 
       $scope.test = !$scope.test; 
     }, 1000); 
    }; 
    $scope.setTimeout = function() { 
     $('body').slideUp(1000, function() { 
      $('body').slideDown(1000, function() { 
       $scope.test = !$scope.test; 
     console.log($scope.test); 
       console.log('done'); 
      }); 
     }); 
    }; 
}); 

为什么$scope变量不发生变化,当我在动画结束后改变?

在超时:

$scope.set = function() { 
    $timeout(function() { 
      $scope.test = !$scope.test; 
    }, 1000); 
}; 

所有工作确定。

+0

角与角JS是不同的,请标注正确 –

回答

1

var app = angular.module('test', []); 
 

 
app.controller('testCtrl', function($scope, $timeout) { 
 
    $scope.test = false; 
 
    $scope.set = function() { 
 
     $timeout(function() { 
 
      $scope.$apply(function() { 
 
       console.log("update time clicked"); 
 
       $scope.test = !$scope.test; 
 
      }); 
 
     }, 1000); 
 
    }; 
 
    $scope.setTimeout = function() { 
 
     $('body').slideUp(1000, function() { 
 
      $('body').slideDown(1000, function() { 
 
       $scope.$apply(function() { 
 
        $scope.test = !$scope.test; 
 
       }); 
 
      }); 
 
     }); 
 
    }; 
 
});
<!DOCTYPE html> 
 
<html ng-app="test"> 
 

 
<head lang="en"> 
 
    <meta charset="utf-8"> 
 
    <title>Wtf is that</title> 
 
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.6.5/angular.min.js"></script> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
 
    <link rel="stylesheet" href="style.css"> 
 
    <script src="script.js"></script> 
 
</head> 
 

 
<body ng-controller="testCtrl"> 
 

 
    {{test}} 
 
    <br /> 
 
    <button ng-click="set()">set</button> 
 
    <button ng-click="setTimeout()">setTimeout</button> 
 

 
</body> 
 

 
</html>

你需要使用$范围$适用,因为 当您更改AngularJS模型之外(从外部JavaScript) - 你需要使用$范围$适用()来AngularJS知道模型已经修改。

+0

但是{{测试}}仍然显示错误 –

+0

答案更新! – 2017-09-02 18:20:52

+0

@MaximShoustin兄弟!你能告诉我我复制了谁吗? – 2017-09-02 18:22:15

0

如果你使用jQuery,角不知道DOM操作。要触发消化周期你可以用$scope.test = !$scope.test;$timeout,使其工作:

$('body').slideUp(1000, function() { 
     $('body').slideDown(1000, function() { 
      $timeout(function(){ 
      $scope.test = !$scope.test; 
      }); 
     }); 
    }); 

Fixed Demo