Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ngFor repeating Components

I have two components one is Post:

import {Component} from '@angular/core';

@Component({
    selector: 'post',
    template: `
    <h1>{{title}}</h1>
    <p>{{postText}}</p>
    `
})
export class Post {
    title : string;
    postText : string;
    constructor(title:string, postText:string) {
        this.title  = title;
        this.postText = postText;
    }
}

the other is newsfeed:

import {Component} from '@angular/core';
import {Post} from "./app.post.component";

@Component({
    selector: 'news-feed',
    template: `
    <h1>News Feed</h1>
    <ul *ngFor='#post of posts'>
        <li *ngFor='#post of posts'>
            {{post | json}}
        </li>
    </ul>
    `
})
export class NewsFeed {
   posts : Post[];
    constructor() {
        let p1 = new Post("Post1","Wow greate post");
        let p2 = new Post("Such Post", "WoW");
        this.posts =[];
        this.posts.push(p1);
        this.posts.push(p2);
    }
}

Is there a way for me to repeat each post using the template in post rather than just using object syntax or formatting the post inside of the newsfeed. Perhaps I am approaching this the wrong way as I am new to ang2. Any help is appreciated.

Thank you very much.

like image 672
Sammy Roberts Avatar asked May 30 '16 07:05

Sammy Roberts


People also ask

Why * is used in ngFor?

In *ngFor the * is a shorthand for using the new angular template syntax with a template tag, this is also called structural Directive.It is helpful to know that * is just a shorthand to explicitly defining the data bindings on a template tag.

Can we use two ngFor in angular?

You can't use multiple *ngFor s in the same element as those are structural directives and angular handles them in a specific way (basically for each structural directive it creates an ng-template element which then holds the directive itself, you can read more about it here: https://angular.io/guide/structural- ...

How do you use ngFor in a tag?

We loop over each person in the people array and print out the persons name. The syntax is *ngFor="let <value> of <collection>" . <value> is a variable name of your choosing, <collection> is a property on your component which holds a collection, usually an array but anything that can be iterated over in a for-of loop.

Can I use ngIf and ngFor together?

Use ngFor and ngIf on same element It's very common scenario where we want to repeat a block of HTML using ngFor only when a particular condition is true. i.e., if ngIf is true.So in this case to use *ngIf and *ngFor on same element, place the *ngIf on a parent element that wraps the *ngFor element.


1 Answers

In fact, Angular2 will instantiate component for you. Simply add the selector tag in your ngFor loop:

<ul>
  <li *ngFor="#postData of posts">
    <post [title]="postData.title"
          [postTest]="postData.postText"></post>
  </li>
</ul>

Your post data must be defined this way:

posts : any[];
constructor() {
  this.posts =[];
  this.posts.push({title:"Post1",postText:"Wow greate post"});
  this.posts.push({title:"Such Post", postText:"WoW"});
}

For this you need to refactor a bit your component to add inputs:

@Component({
  selector: 'post',
  template: `
    <h1>{{title}}</h1>
    <p>{{postText}}</p>
  `
})
export class Post {
  @Input() // <-----
  title : string;
  @Input() // <-----
  postText : string;
}
like image 75
Thierry Templier Avatar answered Oct 13 '22 23:10

Thierry Templier