简体   繁体   English

我需要使用组件 class 中填充的循环将 object 数组动态分配给 angular 中的图像轮播容器

[英]I need to dynamically allocate an object array to an image carousel container in angular using a loop populated within the component class

My issue rests on what I believe to be a misuse of the image carousel html code structure.我的问题在于我认为滥用图像轮播 html 代码结构。 Here is my Html:这是我的 Html:

<div id="carousel-trades" class="carousel slide carousel-fade" data-ride="carousel">
  <div class="carousel-inner" role="listbox">
    <div *ngFor="let ytd of yourTrades; let i = index" class="carousel-item {{ (i == 0) ? 'active' : ''}}">
      <img style="width:310px;height:310px" [src]="splitImageString(ytd.tradeUrl)" (click)="pickImageForTrade(yourTradeImage)"
             alt="First slide">
    </div>
  </div>
</div>

Also I there is the possibility that the array "yourTrades" is not being initialized due to being out of scope.此外,由于不在 scope 范围内,数组“yourTrades”可能未初始化。

component.ts file:组件.ts 文件:

yourTrades: Trade[];

ngOnInit(): void {
this.resetForm();
this.serviceTrade.getUserId()
  .subscribe(data => {

    this.myId = data;

    let yourTrades = new Array();

    for (let i = 0; i < this.trades.length; i++) {
      if (this.myId.userId == this.trades[i].userId) {
        yourTrades.push(this.trades[i]);
      }
    }
    console.log(yourTrades);
  })
}

When I call console.log I get the expected array of objects but when my carousel renders it returns empty.当我调用 console.log 时,我得到了预期的对象数组,但是当我的轮播呈现时,它返回空。 It must be the fact that there may be a wrong connection from when "yourTrades" is populated to when it is used by the html?一定是从“yourTrades”被填充到被 html 使用时可能存在错误连接的事实?

Change your caraousel html to:-将您的旋转木马 html 更改为:-

<div id="carousel-trades" class="carousel slide carousel-fade" data-ride="carousel">
  <div class="carousel-inner" role="listbox">
    <div *ngFor="let ytd of yourTrades; let i = index" class="carousel-item" [ngClass]="{'active': i===0}">
      <img style="width:310px;height:310px" [src]="splitImageString(ytd.tradeUrl)" (click)="pickImageForTrade(yourTradeImage)"
             alt="First slide">
    </div>
  </div>
</div>

You aren't assigning the yourTrades variable inside the subscription to the member variable.您没有将订阅中的yourTrades变量分配给成员变量。 Try the following尝试以下

yourTrades: Trade[] = [];     // <-- assign empty array

this.serviceTrade.getUserId()
  .subscribe(data => {
    this.myId = data;
    for (let i = 0; i < this.trades.length; i++) {
      if (this.myId.userId == this.trades[i].userId) {
        this.yourTrades.push(this.trades[i]);       // <-- directly push to the member variable
      }
    }
    console.log(this.yourTrades);
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在angular 2中使用* ngFor遍历数组中对象的数组? - How do I iterate over an array within an object within an array using *ngFor in angular 2? 如何动态构建一个javascript对象并在嵌套循环中推入数组? - How to dynamically build a javascript Object and push into Array within nested loop? 角度组件:不可能使用 TypeScypt 循环遍历对象数组 - Angular Component: Impossible to loop through an array of object with TypeScypt HTML&CSS-将图像轮播置于其容器的中心 - HTML & CSS - Centering an image carousel within its container ngFor不会在NavigationEnd事件Angular 6+上加载动态填充的数组 - ngFor does not load the dynamically populated array on NavigationEnd event Angular 6+ 如何在 angular 7 中使用 ngFor 循环动态嵌套数组 - how to loop the dynamically nested array using ngFor in angular 7 我在 JS 中重构了一些代码,我需要 su 这个数组中的值,填充字符、布尔值、数字、对象和 arrays - Im refactoring some code in JS and i need to su the values within this array, populated with characters, booleans ,numbers, objects, and arrays 如何将数组转换为数组中的对象 - Angular和Javascript - How do I convert an array into an object within array - Angular and Javascript 如何从一个组件内的数组传递对象? - How do I pass object from array within one component? 我需要循环这个数组 - I need to loop this array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM