首页 > 解决方案 > 如何用 *ng 放置不同的图片?

问题描述

我是 Angular 的初学者,我正在尝试创建一个看起来像 CapitalistAdventure 的应用程序。对于我的产品,我正在使用 *ngFor,但我不知道如何为每个产品提供不同的图片 productName和产品编号。我做了一些研究并尝试了一些东西,但没有任何效果。有人可以解释一下我该怎么做吗?我的应用看起来像这样:

在此处输入图像描述 在此处输入图像描述 这是我的 app.component.html 的代码:

  <h1><span id="WorldName">{{world.name}}WORLDNAME</span></h1>
  <!--<span id="WorldImage"><img [attr.src]="server+world.logo"/></span>-->
  <div class="UserData">
    <div class="Money">
      <span id="MoneyName">Money:</span>
      <br>
      <!--<span [innerHTML]="world.money | bigvalue"></span>-->
      <span>$</span>
    </div>
    <div class="Buy">
      <span id="BuyName">Buy</span>
    </div>
    <div class="ID">
      <span id="MoneyName">ID :</span>
    </div>
  </div>
  <div class="Game">
    <div class="UpgradeContainer">
      <div class="Upgrade" (click)="setModal('Unlock')">Unlocks</div>
      <div class="Upgrade" (click)="setModal('Cash')">Cash</div>
      <div class="Upgrade" (click)="setModal('Angels')">Angels</div>
      <div class="Upgrade" (click)="setModal('Managers')">Managers</div>
      <div class="Upgrade" (click)="setModal('Investors')">Investors</div>
    </div>
    <div class="ProductContainer">
      <app-product *ngFor="let product of products" [product]="product"></app-product>
    </div>
</div>

我的 app.module.ts:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { RestserviceService } from 'src/app/restservice.service';
import { HttpClientModule } from '@angular/common/http';
import { ProductComponent } from './product/product.component';
import { BigvaluePipe } from './bigvalue.pipe';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import {MatProgressBarModule} from '@angular/material/progress-bar';
@NgModule({
  declarations: [
    AppComponent,
    ProductComponent,
    BigvaluePipe,
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    HttpClientModule,
    BrowserAnimationsModule,
    MatProgressBarModule,
  ],
  providers: [RestserviceService],
  bootstrap: [AppComponent]
})
export class AppModule { }

我的 app.component.ts:

 import { Component } from '@angular/core';
import { RestserviceService } from './restservice.service'; 
import { World, Product, Pallier } from './world';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})

export class AppComponent {
  title = 'AngularProject';
  world: World = new World(); 
  server: String;
  products = [1,2,3,4,5,6];
  modal: String;

  constructor(private service: RestserviceService) { 
    this.server = service.getServer(); 
    service.getWorld().then(world => { this.world = world; });   
  }

  setModal(value: String){
    this.modal = value;
  }
}

我的 product.component.html:

<div class="Product">
  <div class="ProductInfo">
    <img onclick="alert('Production lancé'), starFabrication() " class="ProductImage" src="./assets/Avatar.jpg">
    <!--<mat-progress-bar mode="determinate" class="ProgressBar" [value]="progressbarvalue"></mat-progress-bar>-->
    <div class="ProductNumber">ProductNumber</div>
    <div class="ProductPrice">ProductPrice</div>
  </div>
</div>

最后是我的 product.component.ts:

import { Component, OnInit, Input } from '@angular/core';
import { Product } from 'src/app/world';

@Component({
  selector: 'app-product',
  templateUrl: './product.component.html',
  styleUrls: ['./product.component.css']
})
export class ProductComponent implements OnInit {
  product: Product;
  @Input()
  set prod(value: Product){
    this.product = value;
  }

  constructor() { }

  ngOnInit(): void {
  }

}

我的 app.component.css 的代码:

:host{
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  flex-grow: 1;
  background: #282828;
}
h1{
  margin: 0;
  padding: 0;
  font-size: 42px;
  background: -webkit-linear-gradient(rgb(201, 157, 69), rgb(218, 174, 80));
  background-clip: border-box;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  display: flex;
  justify-content: center;
}
.UserData{
  margin: 0;
  padding: 0;
  width: 100%;
  display: flex;
  justify-content: space-between;
}
.ID{
  margin: 0.5em;
  padding: 0.5em;
  background: linear-gradient(120deg, #f7d493, goldenrod);
  color: white;
  text-align: center;
  vertical-align: middle;
  border-radius: 7px;
  box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.39), 0 3px 1px -2px rgba(0, 0, 0, 0.12),
  0 1px 5px 0 rgba(0, 0, 0, 0.507);
}
.Buy{
  margin: 0.5em;
  padding: 0.5em;
  background: linear-gradient(120deg, #f7d493, goldenrod);
  color: white;
  cursor: pointer;
  text-align: center;
  vertical-align: middle;
  border-radius: 7px;
  box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.39), 0 3px 1px -2px rgba(0, 0, 0, 0.12),
  0 1px 5px 0 rgba(0, 0, 0, 0.507);
}
.Money{
  margin: 0.5em;
  padding: 0.5em;
  background: linear-gradient(120deg, #f7d493, goldenrod);
  color: white;
  text-align: center;
  vertical-align: middle;
  border-radius: 7px;
  box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.39), 0 3px 1px -2px rgba(0, 0, 0, 0.12),
  0 1px 5px 0 rgba(0, 0, 0, 0.507);
}
.Game{
  margin: 0;
  padding: 0;
  width: 100%;
  display: flex;
  justify-content: space-between;
  flex-grow: 1;
}
.UpgradeContainer{
  width: 180px;
  justify-content: space-around;
  display: flex;
  flex-direction: column;
}
.Upgrade{
  margin: 0.5em;
  padding: 0.5em;
  background: linear-gradient(120deg, #f5c977, #d3a23f);
  color: white;
  text-align: center;
  border-radius: 7px;
  cursor: pointer;
  box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.39), 0 3px 1px -2px rgba(0, 0, 0, 0.12),
  0 1px 5px 0 rgba(0, 0, 0, 0.507);
}
.ProductContainer{
  padding: 0.1em;
  flex-grow: 1;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-around;
}

最后是我的 product.component.css:

:host {
  padding: 0.5em;
  border: 1px solid #f7b733;
  color: white;
  background: #2d2d2d;
  border-radius: 10px;
  height: 20%;
  flex-basis: calc(50% - 2em - 5px);
  border-radius: 7px;
  box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.747), 0 3px 1px -2px rgba(0, 0, 0, 0.12),
  0 1px 5px 0 rgba(0, 0, 0, 0.767);
}
.ProductImage{
margin: 0;
padding: 0;
width: 100px;
height: 100px;
border-radius: 50%;
cursor: pointer;
}
.ProductProgressBar{
display: inline;
position: relative;
bottom: 40px;
left: 5px;
}
.ProductNumber{
margin: 0.5em;
padding: 0;
border: 2px solid grey;
border-radius: 7px;
color: goldenrod;
display: flex;
flex-direction: row;
justify-content: center;
cursor: pointer;
}
.ProductPrice{
margin: 0.5em;
padding: 0;
border: 2px solid grey;
border-radius: 7px;
color: goldenrod;
display: flex;
flex-direction: row;
justify-content: center;
}

我提前感谢任何愿意帮助我的人:D。

标签: htmlcssangularvisual-studio-codeangular9

解决方案


问题在于您的产品列表只是一个数字数组。它应该是您的产品数组,因此您可以在子组件中访问它的属性。尝试这样的事情:

在你的app.component.ts你应该将产品设置为你想要的产品列表,可能是这样的:

products = [
{ productName: 'name one', productNumber: 21, image: 'url for image' },
{ productName: 'name two', productNumber: 13, image: 'url for image' },
{ productName: 'name three', productNumber: 4, image: 'url for image' }
]

product.component.html,设置这个:

<div class="Product">
  <div class="ProductInfo">
    <img onclick="alert('Production lancé'), starFabrication() " 
      class="ProductImage" [src]="product.image">
    <div class="ProductNumber">{{product.productNumber}}</div>
    <div class="ProductPrice">{{product.productName}}</div>
  </div>
</div>

希望这对你有帮助!


推荐阅读