【问题标题】:Image is not getting displayed in angular2图像未以 angular2 显示
【发布时间】:2017-02-04 16:32:07
【问题描述】:

刚开始使用教程学习 Angular 2。在我的应用程序中,文本正在显示,但对于图像我得到 404 错误。图像文件夹是我的打字稿文件的两个级别,所以我使用了 ../../

我的文件,
食谱.ts

export class Recipe {
    constructor(public name, public description, public imagepath){}
}

recipe-item.component.ts

import {Component,Input} from "@angular/core";    
import {Recipe} from '../recipe'

@Component({
    moduleId:module.id,
    selector: "recipeitem",
    templateUrl: "./recipeitem.html"
})
export class RecipeItemComponent {
    @Input() recipe:Recipe;
}

recipe-item.html

<a href="#" class="list-group-item clearfix">
  <div class="pull-left">
    <h4 class="list-group-item-heading">{{recipe.name}}</h4>
    <p class="list-group-item-text">{{recipe.description}}</p>
  </div>
  <span class="pull-right">
      <img class="img-responsive" src="{{recipe.imagePath}}" alt="No Image"/>
  </span>
</a>

配方列表.component.ts,

import {Component} from "@angular/core";    
import {Recipe} from '../recipe'

@Component({
    moduleId:module.id,
    selector: "recipeList",
    templateUrl: "./recipeList.html"
})
export class RecipeListComponent {
    constructor() {}
    recipes:Recipe[]=[
        new Recipe('Gobi','Very Tasty','../../images/gobi.jpg'),
        new Recipe('jamoon','Sweet','../../images/jamoon.jpg')
    ];
}

recipe-list.component.html

<div class="row">
    <div class="col-xs-12">
        <a class="btn btn-success">New Recipe</a>
    </div>
</div>
<div class="row">
    <div class="col-xs-12">
        <ul class="list-group">
            <recipeitem *ngFor="let recipe of recipes" [recipe] ="recipe"></recipeitem>
        </ul>
    </div>
</div>

【问题讨论】:

  • 这可能只是一个大小写问题。 Recipe 上的属性是 imagepath 但在 recipe-item.html 中绑定是 src="{{recipe.imagePath}}"
  • 我在配方列表文件中创建了一个数组变量,使用我在 html 文件中检索
  • 从您发布的代码中,数组中的食谱将具有属性 imagepath 而不是 imagePath
  • 哦,真是个愚蠢的错误,谢谢指出,请将您的评论转换为答案
  • 没问题,我已将评论移至答案。

标签: angular angular2-template


【解决方案1】:

这可能只是一个外壳问题。 Recipe 上的属性是 imagepath,但在 recipe-item.html 中绑定是 src="{{recipe.imagePath}}"。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-21
    • 1970-01-01
    • 1970-01-01
    • 2018-05-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多