0

它在 alpha 42 上运行良好,但在 beta0 上 ngFor 没有渲染任何东西。组件代码:

import {Component} from 'angular2/core';
import {CORE_DIRECTIVES} from 'angular2/common';
import {ROUTER_DIRECTIVES} from 'angular2/router';

    @Component({
        selector: 'catalog',
        templateUrl: '/src/app/templates/catalog.html',
        directives: [CORE_DIRECTIVES, ROUTER_DIRECTIVES]
    })
    export class CatalogComponent { 
        public products: Object[] = [
            {
                "id": 42,
                "title": "19848",
                "description": "George Orwell's novel of a totalitarian future society in which a man whose daily work is rewriting history tries to rebel by falling in love.",
                "image": "http://u.livelib.ru/reader/jennlawer/o/q2vh8epd/o-o.jpeg"
            }
        ];
        constructor() { }
    }

catalog.html 模板代码:

<h1>test</h1>
<div class="row">
  <div class="col-sm-3" *ngFor="#product of products">
    <div class="card">
      <img class="card-img-top img-responsive" [src]="product.image" alt="Card image cap">
      <div class="card-block">
        <h4 class="card-title">{{product.title}}</h4>
        <p class="card-text">{{product.description}}</p>
        <a [routerLink]="['/Product', {id: product.id}]" class="btn btn-primary">Button</a>
      </div>
    </div>
  </div>
</div>
<router-outlet></router-outlet>

根应用组件

import {Component, provide} from 'angular2/core';
import {bootstrap} from 'angular2/platform/browser';
import {RouteConfig, ROUTER_DIRECTIVES, HashLocationStrategy, LocationStrategy, ROUTER_PROVIDERS} from 'angular2/router';
import {CatalogComponent} from './components/catalog';
import {ProductComponent} from './components/product';
@Component({
    selector: 'app',
    template: '<h1>eShop</h1><router-outlet></router-outlet>',
    directives: [ROUTER_DIRECTIVES]
})
@RouteConfig([
  { path: '/', as: 'Catalog', component: CatalogComponent },
  {path: '/product/:id', as: 'Product', component: ProductComponent},
])
class AppComponent { }
bootstrap(AppComponent, [
    ROUTER_PROVIDERS,
    provide(LocationStrategy, {useClass: HashLocationStrategy})
]);

这个模板包括正确,因为我可以在页面上看到 h1 测试,但我看不到 ngFor 的结果

4

1 回答 1

2

您的示例运行良好(请参阅此 plunk)。

可能你的路由器有问题。确保您已添加ROUTER_PROVIDERSbootstrap函数并已初始化LocationStrategy

import {bootstrap} from 'angular2/platform/browser';
import {provide} from 'angular2/core';
import {CatalogComponent} from './app';
import {ROUTER_PROVIDERS, LocationStrategy, HashLocationStrategy} from 'angular2/router';

bootstrap(CatalogComponent, [
  ROUTER_PROVIDERS,
  provide(LocationStrategy, { useClass: HashLocationStrategy })
]);
于 2015-12-17T05:34:46.473 回答