为了账号安全,请及时绑定邮箱和手机立即绑定

angular2如何知道*ngFor什么时候渲染结束?求老师解惑

import { Component, OnInit } from '@angular/core';
import { Swiper } from '../../../server/declares';
@Component({
  selector: 'app-swiper-adv-1',
  templateUrl: './swiper-adv-1.component.html',
  styleUrls: ['./swiper-adv-1.component.css']
})
export class SwiperAdv1Component implements OnInit {
  bannerSwiper: any;
  items: any = [];
  constructor() {
    
  }

  ngOnChanges(){
    console.log('change');
  }

  _init(){
    this.items.push({
      title: '',
      image: 'http://img.chwlsq.com/1/config/1701151154381248736.jpg'
    });
    this.items.push({
      title: '',
      image: 'http://img.chwlsq.com/1/config/1701151154528930188.jpg'
    });
  }

  ngOnInit() {
    this._init();

    setTimeout(()=>{
      this._init();
    },3000);
    
    this.bannerSwiper = new Swiper('.banner-swiper .list-guanggaowei', {
        loop: true,
        speed: 1000,
        autoplay: 2000,
        autoplayDisableOnInteraction: false,
        pagination: '.banner-swiper .swiper-pagination'
    });
  }

}


正在回答

1 回答

import { Component, OnInit } from '@angular/core';
import { Swiper } from '../../../server/declares';
@Component({
  selector: 'app-swiper-adv-1',
  templateUrl: './swiper-adv-1.component.html',
  styleUrls: ['./swiper-adv-1.component.css']
})
export class SwiperAdv1Component implements OnInit {
  bannerSwiper: any;
  items: any = [{
      title: '',
      image: 'http://img.chwlsq.com/1/config/1701151154381248736.jpg'
    },{
      title: '',
      image: 'http://img.chwlsq.com/1/config/1701151154528930188.jpg'
    },{
      title: '',
      image: 'http://img.chwlsq.com/1/config/1701151154528930188.jpg'
    },{
      title: '',
      image: 'http://img.chwlsq.com/1/config/1701151154528930188.jpg'
    }];
  _index: number = 0;
  constructor() {
    
  }

  getLast(last){
    this._index ++;
    console.log('last is'+this._index,last);
  }

  ngOnChanges(){
    console.log('change');
  }

  ngOnInit() {
    let last = false;
    if(last){
      this.bannerSwiper = new Swiper('.banner-swiper .list-guanggaowei', {
          loop: true,
          speed: 1000,
          autoplay: 2000,
          autoplayDisableOnInteraction: false,
          pagination: '.banner-swiper .swiper-pagination'
      });
    }
  }

}
<div class="indexBox">
    <section class="container banner-swiper">
        <div class="list-guanggaowei">
            <div class="swiper-wrapper">
                <div class="swiper-slide" *ngFor="let item of items;index as i;last as isLast;">
                    <img [src]="item.image">
                    <span *ngIf="getLast(isLast)">{{i}}</span>
                </div>
            </div>
            <div class="swiper-pagination"></div>
        </div>
    </section>
    <div class="water">
        <div class="water-c">
            <div class="water-1"></div>
            <div class="water-2"></div>
        </div>
    </div>
</div>

http://img1.sycdn.imooc.com//58dd33020001f33406240776.jpg



为什么一个ngFor要执行这么多次,四次! 这个貌似不太合理吧!

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
AngularJS实战
  • 参与学习       205365    人
  • 解答问题       1225    个

一起学习AngularJS的基础教程,通过实例学习并学会AngularJS

进入课程

angular2如何知道*ngFor什么时候渲染结束?求老师解惑

我要回答 关注问题
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号