简体   繁体   English

使用* ngFor迭代组件

[英]iterate component using *ngFor

I am facing problem generating components using ngFor directive in angular2. 我在angular2中使用ngFor指令生成组件时遇到问题。

My desired component model tree is: RootComponent ArticlesComponent Article_1 Article_2 Article_N 我期望的组件模型树是:RootComponent ArticlesComponent Article_1 Article_2 Article_N

**RootComponent.html**
<section>Html for root component goes here</section>
<articles>This contains the articles html and will act as container for enumerated article tags</articles>

**Articles.component.ts**


 import {Component} from "@angular/core";
 import {ArticleComponent} from "./article.component";


 @Component({
             moduleId : module.id,
             selector : 'articles-list',
             template : `<div class="row">
                           <ul>
                             <li *ngFor="let article of articles" >
                                       <article [title]="article.title"
                                       [link]="article.link"></article>
                             </li>
                           </ul>
                        </div>`
           })


 export class ArticleListComponent{

     public articles :ArticleComponent[];

     constructor(){
        this.articles = [];
        let articleObj1 = new ArticleComponent("title1","link1");
        let articleObj2 = new ArticleComponent("title2","link2");
        this.articles.push(articleObj1);
        this.articles.push(articleObj2);
     }

  }

  **Article.component.ts**

   import {Component,Input} from "@angular/core";

   @Component({
               moduleId: module.id,
               selector: 'article',
               templateUrl: `<div class ="row">
                               <h3>{{title}}</h3>
                               <h5><a href="#" >{{link}}</a></h5> 
                             </div>`
             })


   export class ArticleComponent {

         @Input() title : string;
         @Input() link : string;

         public count:number;

         constructor(title,link){
                this.title = title;
                this.link = link;
         }

         incrementCount(){
            this.count++;
         }


     }

System.js throws an error : System.js引发错误:

(SystemJS) Can't resolve all parameters for ArticleComponent: (?, ?).↵  Error: Can't resolve all parameters for ArticleComponent: (?, ?).↵

What does this error actually mean and how to rectify this? 该错误实际上是什么意思,以及如何纠正该错误?

ArticleComponent is instantiated by Angulars DI. ArticleComponent由Angulars DI实例化。 If it has parameters, they need to be resolvable by DI. 如果具有参数,则它们必须由DI解析。 title and link are not resolvable, because there are no providers. titlelink无法解析,因为没有提供者。 Parameters without type annotations also would need an @Injectable() decorator. 没有类型注释的参数也将需要@Injectable()装饰器。

I think the right approach is to remove the parameters from the constructor and instead use only the inputs. 我认为正确的方法是从构造函数中删除参数,而仅使用输入。

 export class ArticleComponent {

     @Input() title : string;
     @Input() link : string;

     public count:number;

     /*constructor(title,link){
            this.title = title;
            this.link = link;
     }*/

     incrementCount(){
        this.count++;
     }
 }

also the constructor in ArticleListComponent can be shortened, because as mentioned DI creates the component instances. 同样, ArticleListComponent的构造ArticleListComponent也可以缩短,因为如上所述,DI创建了组件实例。

 export class ArticleListComponent{

     public articles :ArticleComponent[];

     /*
     constructor(){
        this.articles = [];
        let articleObj1 = new ArticleComponent("title1","link1");
        let articleObj2 = new ArticleComponent("title2","link2");
        this.articles.push(articleObj1);
        this.articles.push(articleObj2);
     }*/
  }

all you need is the data for *ngFor , the rest is done by <article> 您需要的只是*ngFor的数据,其余的由<article>

 export class ArticleListComponent{

     public articles :ArticleComponent[];

     constructor(){
        this.articles = [{title: "title1",link: "link1"}, {title: "title2",link: "link2"}];
     }
  }

As per your code , just change 根据您的代码,只需更改

let articleObj1 = {"title":"title1","link" : "link1"};
let articleObj2 = {"title":"title2","link" : "link2"};

And you will get , desired output. 然后您将获得所需的输出。

There is no need to do : 无需这样做:

let articleObj1 = new ArticleComponent("title1","link1");
let articleObj2 = new ArticleComponent("title2","link2");

Error is related to the ArticleComponent("title1","link1") , Your ArticleComponent doesn't have those two params in constructor. 错误与ArticleComponent("title1","link1") ,您的ArticleComponent在构造函数中没有这两个参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM