【问题标题】:ng-animate in ng-if is not working when changing ng-if condition asynchronously (in callback function)ng-if 中的 ng-animate 在异步更改 ng-if 条件时不起作用(在回调函数中)
【发布时间】:2014-08-13 01:40:34
【问题描述】:

当我在正常功能中更改 ng-if 条件时,我可以使 ng-animate 在 ng-if 中工作,例如

我的看法:

 <div ng-if="!show">
   <a ng-click="showWorld()">Hello</a> 
 </div>
 <div ng-if="show" class="animation">
   Hello world!
 </div>

我在指令中的功能:

 scope.showWorld = function() {
     scope.show = true;
 }

当我调用 showWorld() 函数来改变 ng-if 条件,Hello world!会出现动画,但如果我这样做:

 scope.showWorld = function() {
     Post.test(params, function(){
        scope.show = true;   
     })
 }

Post 是有角度的 $resource,例如: $resource('api/post', {}, {test: {method: 'POST'}})

而且 scope.show 仍然会改变,Hello world!仍然显示,但动画不工作...

我在网上找了半天也没找到答案,谁能告诉我为什么??

非常感谢!!

【问题讨论】:

    标签: angularjs ng-animate


    【解决方案1】:

    您可能需要围绕您的操作函数调用 $scope.$apply(),例如:

    scope.showWorld = function() {
         Post.test(params, function(){
            $scope.$apply(function () {
                scope.show = true;   
            });  
         })
     }
    

    您也可以使用$scope.$evalAsync(function(){}) 作为替代

    【讨论】:

    • 感谢您的回答!我以前尝试过这种方式,但收到“错误:$rootScope:inprog Action Already In Progress $digest already in progress”错误消息。
    • 查看我关于使用 $evalAsync 的评论 - 它可能会有所帮助。
    • 嗨,我尝试了 $evalAsync 但仍然无法正常工作......奇怪的是它似乎在 Safari 中工作,但在 chrome 中却没有......
    猜你喜欢
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 2019-08-06
    • 1970-01-01
    • 2013-06-07
    • 2016-08-13
    • 2017-07-25
    相关资源
    最近更新 更多