1

I have this code:

HTML code:

<!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 code:

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

Why $scope variables doesn't change when I changing after the animation ends?

In timeout:

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

All working ok.

1
  • Angular and Angular js are different please tag correctly Commented Sep 2, 2017 at 18:29

3 Answers 3

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>

you need to use $scope.$apply because when you change an AngularJS model outside (from external JavaScript) - you need to use $scope.$apply() to AngularJS know that model has modified.

Sign up to request clarification or add additional context in comments.

7 Comments

But {{test}} still displays false
Take a look on @eKimiya answer.
i have added timeout before but the problem occurs because i have not added $scope.$apply that is why not working
its ok, just give few words why we need $apply, not just working solution. Its not good practice in SO :)
please ask polak polkovski my answer not give him real time changethat is the reason i have changed answer
|
0

Because jQuery animations are running outside of Angular scope. You need to specify to Angular that your scope has changed with :

$scope.$apply();

https://plnkr.co/edit/hfIlZzBXo9hTmIvJ15oa?p=preview

Comments

0

If you use jQuery, Angular doesn't know about DOM manipulations. To trigger digest cycle you can wrap $scope.test = !$scope.test; with $timeout to make it work:

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

Fixed Demo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.