te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - When an Angular 2 component has fully loaded - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - When an Angular 2 component has fully loaded - Stack Overflow

programmeradmin3浏览0评论

I cannot seem to find a hook by which I can know that a ponent that is made up of other child ponents has pletely finished loading including data-binding and everything. For example take the following template for ParentComponent:

<div>
    <child-one>
        {{textOne}}
    </child-one>
    <child-two>
        {{textTwo}}
    </child-two>
</div>

Any suggestion is highly appreciated.

I cannot seem to find a hook by which I can know that a ponent that is made up of other child ponents has pletely finished loading including data-binding and everything. For example take the following template for ParentComponent:

<div>
    <child-one>
        {{textOne}}
    </child-one>
    <child-two>
        {{textTwo}}
    </child-two>
</div>

Any suggestion is highly appreciated.

Share Improve this question asked May 22, 2016 at 12:15 lbrahimlbrahim 3,81012 gold badges59 silver badges98 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 4

When ngAfterViewInit() {} was called or if content is passed (<ng-content>), after ngAfterContentInit() was called.


  • ngAfterViewInit() is called after the view initialization is pleted. The ponent is fully loaded and initialized at this state.
  • If also children passed to the ponent should be fully loaded then use ngAfterContentInit() instead.

It's wrong! ngAfterViewInit is fired when the view is being initialized... Put a breakpoint into it and you will see. A hook to know when the view is actually fully loaded is really missing for Angular 2! But as a workaround you could try something like that:

ngAfterViewInit() {
    console.log('View is being initialized');
    console.log(this.el.nativeElement.offsetHeight);

    setTimeout(() => {
        console.log('View is fully loaded');
        console.log(this.el.nativeElement.offsetHeight);
    }, 0);
}

Building on Dimitri's answer, I've found that even after ngAfterViewInit is called, some elements were still not pletely loaded, in my case I was after the scrollHeight of an element.

Using Ionic's platform.ready() and some timeout-testing, I noticed content finally being instantiated after about 60ms, so I increased it to 250ms just provide some wiggle-room. It's really not ideal but it's the last lifecycle hook to perform checks... but maybe it'll help someone?

constructor(public platform: Platform) {}

ngAfterViewInit() {
    this.platform.ready().then(() => {
        /* still returns 0 here */
        console.log(this.el.nativeElement.scrollHeight);

        setTimeout(() => {
            /* returns x here */
            console.log(this.el.nativeElement.scrollHeight);
        }, 250);
    });
}

I know not everyone'll be using Ionic to have the Platform class available, it was only relevant to my use case as I was using ion-icons. But I left it in, in case it helps another Ionic user or something...

Question being asked for Angular2... but these hooks are still in action on 1st Jan 2024.

ngAfterViewInit is the one mostly heared about, but if you are looking for a hook that register the event of all the templates getting finally rendered then ngAfterViewChecked is something you are looking for.

There are couple of more hooks including ngAfterContentInit and ngAfterContentChecked which can be explored here https://angular.io/guide/lifecycle-hooks.

Hope this helps, cheers!

发布评论

评论列表(0)

  1. 暂无评论