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.
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.
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- ...
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.
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.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With