angular - bxslider 在角度 6 中不能与 *ngFor 一起使用
问题描述
我在 Angular 6 中较新并使用 bxslider。它与数组一起工作正常,但我使用 *ngFor 作为动态它不起作用。如何重新初始化动态数据。
使用数组正常工作
<div class="bxslider">
<div *ngFor="let slide of sliders"><img [src]="slide.url"></div>
</div>
动态不工作
<div class="bxslider">
<div *ngFor="let slide of banner_images"><img [src]="slide.data.image"></div>
</div>
这些是 component.ts
import { Component, OnInit, AfterViewInit } from '@angular/core';
import { HomeHttpService } from 'src/app/shared/services/home/home-
http.service';
declare var jquery:any;
declare var $ :any;
@Component({
selector: 'app-top-savers',
templateUrl: './top-savers.component.html',
styleUrls: ['./top-savers.component.css']
})
export class TopSaversComponent implements OnInit, AfterViewInit {
public banner_images: any[];
//static calling
public sliders : any[] = [
{"url":"image.jpg"},
{"url":"image.jpg"},
{"url":"image.jpg"},
{"url":"image.jpg"},
{"url":"image.jpg"}
];
constructor(private homeService:HomeHttpService) { }
ngOnInit() {
//fetching api data
this.homeService.getBanner().subscribe((data) =>{
let banner = data[2]["objects"];
this.banner_images = banner;
//console.log(this.banner_images);
});
}
ngAfterViewInit() {
$('.bxslider').bxSlider({
auto:true
});
}
}
请找到下面的图片 https://i.imgur.com/UUp6YFk.jpg
解决方案
请将 OnInit 挂钩更改为 AfterViewInit。
import { Component, AfterViewInit} from "@angular/core";
declare var jquery:any;
declare var $ :any;
@Component({
selector: "app-banner",
templateUrl: './banner.component.html',
styleUrls: ['./banner.component.css']
})
export class BannerComponent implements AfterViewInit {
sliders = [
{"url":"/images/image5.jpg"},
{"url":"/images/image4.jpg"},
{"url":"/images/image6.jpg"}
];
constructor() {}
ngAfterViewInit() {
$('.slider').bxSlider({
auto:true
});
}
}
推荐阅读
- php - 如何修复使用 PHP 运行的 ffmpeg 中的“无法找到适合 '/' 的合适输出格式”错误
- java - 如何在 Websphere 8.5.5 上的 SOAP 响应中发送 CDATA 部分
- linux - Ubuntu Docker 容器立即停止,Dockerfile 有问题吗?
- reactjs - 如何在 typescript react 的 bpmn-js/lib/Modeler 中获取 XML?
- angular - 树组件未正确呈现
- c# - 为什么 .net modulo 与 Windows calcuator 对小数的工作方式不同?
- node.js - 具有express框架的nodejs中的acl权限
- oauth-2.0 - 如何在 GSuite 中创建 OAuth 2.0 应用程序仅供内部使用?
- swift - 在 Swift 4 中将可选字符串转换为 Int
- elixir - 如何有一个键值输出