Javascript 避免$timeout()强制以角度完成摘要
在我的Angular应用程序中,需要发生以下事件序列:Javascript 避免$timeout()强制以角度完成摘要,javascript,jquery,angularjs,scroll,angularjs-digest,Javascript,Jquery,Angularjs,Scroll,Angularjs Digest,在我的Angular应用程序中,需要发生以下事件序列: 用户单击一个按钮 通过翻转绑定了ng show的布尔范围变量来显示先前隐藏的div 新显示的视图(位于页面下方)应该滚动到视图中 目前,滚动不起作用,因为这都发生在同一个摘要中。这意味着ng show绑定变量的新状态没有机会更新DOM。因此,我们试图滚动到一个元素,就DOM而言,它还不可见 这可以通过使用$timeout包装scroll调用来缓解,这将强制所有摘要在尝试滚动之前完成。虽然这样做有效,但感觉像是一个黑客,我想知道是否有更好的方
<div ng-app="app" ng-controller="MyCtrl" id="container">
<button ng-click="scroll()">Unhide element and scroll</button> (will not work until $timeout call is uncommented)
<div style="height:1000px; background-color: #ddddd0">
</div>
<div id="target" ng-show="isVisible">section to scroll to</div>
</div>
angular.module("app", [])
.controller('MyCtrl',
function MyCtrl($scope, $timeout) {
$scope.isVisible = false;
$scope.scroll = function() {
$scope.isVisible = true;
// uncommenting the $timeout fixes it, but feels like a hack
//$timeout(function() {
$('body').animate({
scrollTop: $("#target").offset().top
}, 500);
//});
};
}
);
使用$timeout不是什么大问题,因为您正在运行的代码不是angular代码,您需要以某种方式与angular通信,以便刷新视图。您认为使用angular动画模块会更理想吗?这能处理这种情况吗?不确定是否可以使用角度动画。。但是,即使可以,我认为您的场景太琐碎,无法使用动画模块。但我可能错了,所以在这种情况下不要相信我的话。