angularjs - Angular 1.4.0 $timeout is not happening after ng-show has taken effect with ngAnimate -
i upgraded angular 1.4.0 , having timing issues ng-show , $timeout , i’m not sure best way solve is.
i created plunkr demonstrates situation: http://plnkr.co/edit/griup3mh6kzazcrfhcey?p=preview
app.controller('mainctrl', function($scope, $timeout) { $scope.output = ''; $scope.toggle = false; var outputstuff = function() { $timeout(function() { document.getelementbyid("output").innerhtml += "<strong>-----first div shown:" + $scope.toggle + ", second div shown:" + !$scope.toggle + "--------------</strong><br>"; var firstdivclass = angular.element(document.getelementbyid("firstdiv")).attr("class"); var seconddivclass = angular.element(document.getelementbyid("seconddiv")).attr("class"); var firstdivoffsetwidth = document.getelementbyid("firstdiv").offsetwidth; var seconddivoffsetwidth = document.getelementbyid("seconddiv").offsetwidth; document.getelementbyid("output").innerhtml += "first div class:" + firstdivclass + "<br>"; document.getelementbyid("output").innerhtml += "second div class:" + seconddivclass + "<br>"; document.getelementbyid("output").innerhtml += "first div width:" + firstdivoffsetwidth + "<br>"; document.getelementbyid("output").innerhtml += "second div width:" + seconddivoffsetwidth + "<br>"; if ($scope.toggle && firstdivoffsetwidth === 0) { document.getelementbyid("output").innerhtml += "<span style='color:red'>unexpected!!</span><br>"; } if (!$scope.toggle && seconddivoffsetwidth === 0) { document.getelementbyid("output").innerhtml += "<span style='color:red'>unexpected!!</span><br>"; } }); }; $scope.togglevariable = function() { $scope.toggle = !$scope.toggle; outputstuff(); }; }); basically, when click something, changing scope variable ng-show depends on. when element shown, trying element width. however, i’m observing inconsistent. element showing hasn’t been shown yet. therefore width 0.
if remove nganimate module app, works expected; element set ng-show=“true”, in fact shown , has width.
this used work fine in angular 1.3.14 , seems work way 1.4.0-rc.2
in plunkr, can click on "click me" button bunch of times , see red "unexpected!!" text show up. when element supposed shown isn't , has width of 0.
you can remove 'nganimate' module , see red text never appears. can leave 'nganimate' in , change angular version in index.html code.angularjs.org/1.4.0-rc.2/angular.js , code.angularjs.org/1.4.0-rc.2/angular-animate.js , see red text not appear. used google chrome test this.
why timing inconsistent , if $timeout not right way wait element rendered, correct way it?
i can't reproduce error demo. however, came across similar once before. iirc, $timeout used use angular's $evalasync queue, may not guaranteed run after next redraw. this changed recently. try using $window.settimeout instead.
Comments
Post a Comment