【发布时间】:2016-03-27 11:28:13
【问题描述】:
我目前有以下组件树,
app.component
|-> top-bar.component
|-> menu-bar.component
|-> bottom-bar.component
|-> content-area.component
如果 content-area 组件溢出并显示滚动条,我的 bottom-bar 组件会更改其行为。我目前在 bottom-bar 中使用 jQuery 来查找 content-area 组件并检查是否显示滚动条。但是,为了避免这种紧密耦合,我更愿意将 content-area 组件(或其接口)注入到 bottom-bar 组件中,以便我不必依赖具有 CSS 类的特定 DIV。
实现这一目标的最佳方法是什么?
[编辑]
我已经尝试了下面的示例,这是我想要实现的目标(contentarea 已经在我的@Component 输入数组中),
export class BottomBarComponent implements AfterViewInit {
constructor(private el:ElementRef, private contentarea:ContentAreaComponent
{
}
ngAfterViewInit() {
console.log(this.contentarea);
var gilCounter = jQuery(this.el.nativeElement).find('.gilcounter');
var contentArea =jQuery(this.contentarea.nativeElement);
var lastHasScrollBar = false;
setInterval(function() {
var currentHasScrollBar = contentArea.hasScrollBar();
if (currentHasScrollBar != lastHasScrollBar) {
if (currentHasScrollBar) {
gilCounter.css('right', scrollbarWidth + 'px');
} else {
gilCounter.css('right', '0px');
}
lastHasScrollBar = currentHasScrollBar;
}
}, 300);
}
}
【问题讨论】:
-
对于您的情况,您应该使用bidirectional service