2016-02-21 104 views
1

我所试图做的事:我基本上是试图遍历products变量,显示内容为行。Angular2(打字稿)ngFor不工作

问题:问题基本上是它似乎没有工作。它所做的,当我加入ngFor的是,它完全隐藏应用程序的角部位。这意味着,有一个错误。当我检查控制台时,它似乎没有显示任何有用的信息(看起来像Angular2中还没有友好的消息)。

我试过了:我试过导入CORE_DIRECTIVES并将它作为指令传递,但是没有奏效。我也试图消除整个ngFor然后应用程序的其他部分似乎完美的工作。

我想要的是:如果您可以查看我的代码并让我知道我的错误是什么,我将非常感激。先谢谢你。

所以,这是app.ts

import { bootstrap } from "angular2/platform/browser"; 
import { Component } from "angular2/core"; 
import { Product } from './models.ts'; 

@Component({ 
    selector: 'table-rows', 
    template: ` 
     <tr *ngFor="#product of products"> 
      <td>{{ product.name }}</td> 
      <td>{{ product.quantity }}</td> 
      <td>{{ product.unitPrice }}</td> 
     </tr> 
    ` 
}) 
class TableRows { 
    products: Product[]; 

    constructor() { 
     this.products.push(new Product('GALAXY Note 3', 10, 500)); 
     this.products.push(new Product('MacBook Pro', 25, 1500)); 
    } 
} 

@Component({ 
    selector: 'app', 
    directives: [TableRows], 
    template: ` 
     <table class="table"> 
      <thead> 
       <tr> 
        <td>Product Name</td> 
        <td>Product Quantity</td> 
        <td>Product Unit Price</td> 
       </tr> 
      </thead> 
      <tbody> 
       <table-rows></table-rows> 
      </tbody> 
     </table> 
    ` 
}) 
class App { 
    constructor() { 

    } 
} 

bootstrap(App); 

这里的models.ts

export class Product { 
    name: string; 
    quantity: number; 
    unitPrice: number; 

    constructor (
     name: string, 
     quantity: number, 
     unitPrice: number   
    ) { 
     this.name = name; 
     this.quantity = quantity; 
     this.unitPrice = unitPrice; 
    } 
} 

最后,这里的index.html的:

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
     <meta charset="utf-8"> 
     <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
     <meta name="viewport" content="width=device-width, initial-scale=1"> 

     <title>Stock Table</title> 

     <link href="css/bootstrap.min.css" rel="stylesheet"> 

     <!--[if lt IE 9]> 
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script> 
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script> 
     <![endif]--> 

     <script src="../node_modules/es6-shim/es6-shim.js"></script> 
     <script src="../node_modules/angular2/bundles/angular2-polyfills.js"></script> 
     <script src="../node_modules/systemjs/dist/system.src.js"></script> 
     <script src="../node_modules/rxjs/bundles/Rx.js"></script> 
     <script src="../node_modules/angular2/bundles/angular2.dev.js"></script> 

    </head> 
    <body> 
     <script> 
      System.config({ 
      packages: {   
       app: { 
       format: 'register', 
       defaultExtension: 'js' 
       } 
      } 
      }); 
      System.import('ts/app.js') 
       .then(null, console.error.bind(console)); 
     </script> 

     <nav class="navbar navbar-default navbar-static-top"> 
      <div class="container"> 
       <div class="navbar-header"> 
        <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar"> 
        <span class="sr-only">Toggle navigation</span> 
        <span class="icon-bar"></span> 
        <span class="icon-bar"></span> 
        <span class="icon-bar"></span> 
        </button> 
        <a class="navbar-brand" href="#">Stock Table</a> 
       </div> 
       <div id="navbar" class="collapse navbar-collapse"> 

       </div> 
      </div> 
     </nav> 

     <div class="container"> 
      <app></app> 
     </div> 

     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
     <script src="js/bootstrap.min.js"></script> 
    </body> 
</html> 

回答

1

你必须用来初始化数组命名为products

products: Product[]= []; 

因为你还没有初始化你的数组,所以解释器会抛出错误push of undefined。您必须在使用前初始化阵列。

你可以找到这里的解决方案工作plunker。

http://plnkr.co/edit/NpiCIsI88A1HmW2zC8rm?p=preview

0

例如:

*ngfor = "let number of numbers" 

可能出现的错误:

  1. 拼写检查,
  2. 数组初始化,
  3. 不当HTML语法。