【问题标题】:ngx-bootstrap accordion open panel dynamicallyngx-bootstrap 手风琴动态打开面板
【发布时间】:2017-09-13 19:04:30
【问题描述】:

我使用 ngx-bootstrap 手风琴来显示博客文章列表。

这是模板:

<accordion id="blog-list">
    <accordion-group *ngFor="let post of posts; let first = first;" [isOpen]="first" id="post-{{post.id}}">
        <!-- Here goes content irrelevant to the question -->
    </accordion-group>
</accordion>

我还使用了一些全局配置,一次只能打开一个手风琴面板。

export function getAccordionConfig(): AccordionConfig {
  return Object.assign(new AccordionConfig(), { closeOthers: true });
}

现在,当帖子更新时,我会在列表中更新它,如下所示:

constructor(private elementRef: ElementRef, private postService: PostService) {
    this.postService.updatedPost.subscribe(val => {
      let i = this.posts.findIndex(post => post.id === val.id);
      this.posts[i] = val;
      let element = elementRef.nativeElement.querySelector('#post-' + val.id);
      element.setAttribute('isOpen', true); // <- this does not work
      element.scrollIntoView(true);
    });
  }

更新和滚动工作正常,但我不知道如何打开面板。视图更新并滚动后,所有面板都将关闭。我希望打开更新帖子的面板。

【问题讨论】:

标签: javascript angular ngx-bootstrap bootstrap-accordion


【解决方案1】:

所以问题在[isOpen]="first",默认打开第一个帖子 使用 DOM 直接操作不会触发绑定更新

你可以做的是:

[isOpen]="activPostIndex === index"

activPostIndex = 0;
constructor(private elementRef: ElementRef, private postService: PostService) {
    this.postService.updatedPost.subscribe(val => {
      this.activPostIndex = this.posts.findIndex(post => post.id === val.id);
      this.posts[i] = val;
    });
  }

【讨论】:

  • 完美运行。谢谢!
猜你喜欢
  • 2020-05-03
  • 2021-07-07
  • 2019-07-23
  • 1970-01-01
  • 2017-08-17
  • 2015-10-08
  • 2015-01-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多