1

I have a portfolio item that is being used in a parent component, web-portfolio.html, getting data from web-portfolio.ts.

I'm trying to pass data from an array in the web-portfolio.ts file in an *ngFor loop.

<app-portfolio *ngFor = "let site of this.sites" name = "site.name" img = "site.img" description = "site.description" link = "site.link"></app-portfolio>

The app-portfolio component has inputs for these:

export class PortfolioComponent implements OnInit {

  isOpen: boolean = false;
  @Input() name: string;
  @Input() img: string;
  @Input() description: string;
  @Input() link: string;

}

But instead of getting the correct output, I receive three of the initial array's value;

export class WebPortfolioComponent implements OnInit {
 sites = [
    {
     "name":"Site A",
     "description": "Lorem Ipsum",
      "img":"/web/sitea.png",
      "link":"http://sitea.com"
    },
    {
     "name":"Site B",
     "description": "Lorem Ipsum",
      "img":"/web/siteb.png",
      "link":"http://siteb.com"
    },
   {
     "name":"Site C",
     "description": "Lorem Ipsum",
      "img":"/web/siteC.png",
      "link":"http://siteC.com"
    },
  ];
}

 // OUTPUT => Blank, blank, blank

What am I doing wrong?

portfolio.component.html:

<div class = "portfolio-item">
  <div class = "portfolio-thumb">
    <a (click) = "this.isOpen = !this.isOpen">
      <img src = "" alt = ""/>
    </a>
  </div>
  <div class = "portfolio-full" [ngClass] = "{'port-show':this.isOpen}">
    <div class = "portfolio-full-image" (click) = "this.isOpen = !this.isOpen">
      <img src = "{{this.img}}">
      <div class = "portfolio-description">
        {{this.description}}
      </div>
    </div>
  </div>
</div>
6
  • 2
    Share the HTML code of PortfolioComponent. Commented Jun 26, 2019 at 3:42
  • Is it a typo to write name not [name]? Commented Jun 26, 2019 at 3:43
  • Neither name nor [name] seems to work. Which is correct, [name]? @pindev Commented Jun 26, 2019 at 3:46
  • 5
    It should be either name="{{ site.name }}" or [name]="site.name" Commented Jun 26, 2019 at 3:47
  • 1
    Your code looks correct when you change your html code as @pindev's comment. I was wondering whether the value of sites was changed somewhere. Try to write <p>{{ sites | json }}</p> in your html to print out the value of it. And you don't need to write this. in html. You can just use img, name and etc. Commented Jun 26, 2019 at 3:53

3 Answers 3

1

You need add {{}} for property component name = "{{site.name}}"

<app-portfolio *ngFor = "let site of this.sites" name = "{{site.name}}" 
 img = "{{site.img}}" description = "site.description" link = "site.link"></app-portfolio>

https://stackblitz.com/edit/angular-ns1qli

Update for your HTML still worked

https://stackblitz.com/edit/angular-input-parameter?file=src/app/portfolio.html

Sign up to request clarification or add additional context in comments.

2 Comments

That doesn't seem to make any difference, unfortunately
Looks like I'd made a mistake leaving out the image src in the HTML. Thank you for your demo!
1

Just wrap the component like property binding in Angular

<app-portfolio *ngFor = "let site of this.sites" [name] = "site.name" [img] = "site.img" [description] = "site.description" [link] = "site.link"></app-portfolio>

Comments

0

The img is an html attribute so it cannot be used with @Input so please use [image] instead, please check the below example.

Stack Blitz Demo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.