您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

AngularJS:观察高度变化的更好方法

AngularJS:观察高度变化的更好方法

这通过注册一个emHeightSource名为every 的观察者来工作$digest。它会更新该__height属性,该属性又将在监视emHeightTarget

/*
 * Get notified when height changes and change margin-top
 */
.directive( 'emHeightTarget', function() {
    return {
        link: function( scope, elem, attrs ) {

            scope.$watch( '__height', function( newHeight, oldHeight ) {
                elem.attr( 'style', 'margin-top: ' + (58 + newHeight) + 'px' );
            } );
        }
    }
} )

/*
 * Checks every $digest for height changes
 */
.directive( 'emHeightSource', function() {

    return {
        link: function( scope, elem, attrs ) {

            scope.$watch( function() {
                scope.__height = elem.height();
            } );
        }
    }

} )
其他 2022/1/1 18:15:27 有573人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶