【问题标题】:ngFor is not working angular 2 and display blank pagengFor 不工作 angular 2 并显示空白页
【发布时间】:2017-01-15 14:29:41
【问题描述】:

我观看了 youtube 视频,并尝试完全按照此链接 https://www.youtube.com/watch?v=bKWDKmHvF78&index=8&list=PL6gx4Cwl9DGBYxWxJtLi8c6PGjNKGYGZZ 上的视频进行操作,但 ngFor 根本不适合我。 当我用 ngFor 写作时,它会显示空白页。

//app.component.ts

import {Component} from 'angular2/core';
import {Config} from './config.service';
import {Video} from './Video';
import {PlaylistComponent} from './playlist.component';

@Component({
    selector: 'my-app',
    templateUrl: 'app/ts/app.component.html',
    directives:[PlaylistComponent]
})

export class AppComponent {
    name="check";
    videos:Array<Video>;
    constructor(){
        this.videos=[
            new Video(1,"row1","check1","this is our first row"),
            new Video(1,"row2","check2","this is our second row"),
        ]
    }
}

//app.component.html

<h1>{{name}}</h1>
<playlist [videos]="videos"></playlist>

//main.ts

import {bootstrap}    from 'angular2/platform/browser';
import {AppComponent} from './app.component';

bootstrap(AppComponent);

//playlist.component.html

<table class="table table-hover">
    <thead>
    <tr>
        <td>ID</td>
        <td>Title</td>
        <td>Description</td>
    </tr>
    </thead>
    <tbody>
        <tr  *ngFor="let v of videos">
            <td>{{v.id}}</td>
            <td>{{v.title}}</td>
            <td>{{v.desc}}</td>

        </tr>
</tbody>

</table>

//playlist.component.ts

/**
 * Created by Adir on 9/7/2016.
 */
import {Component} from 'angular2/core';
import {Video} from './Video';

@Component({
    selector:'playlist',
    templateUrl:'app/ts/playlist.component.html',
    inputs:['videos']
})

export class PlaylistComponent{
    onSelect(vid:Video){
        console.log(JSON.stringify(vid));
    }
}

【问题讨论】:

  • 您需要一个带有@Input() 装饰器的属性才能将视频作为参数传递。
  • 我有这个 '/** * 由 Adir 于 2016 年 9 月 7 日创建。 */ 从 'angular2/core' 导入 {Component};从'./Video'导入{Video}; @Component({ selector:'playlist', templateUrl:'app/ts/playlist.component.html', inputs:['videos'] }) export class PlaylistComponent{ onSelect(vid:Video){ console.log(JSON.字符串化(vid)); } } '
  • 您不需要输入:[视频]。而是声明 @Input() 视频;在 playlist.component.
  • 怎么写呢? @输入(视频); ??

标签: angularjs angular ngfor


【解决方案1】:

从组件定义中移除输入并在类中添加输入属性。

import {Input} from '@angular/core';
export class PlaylistComponent{
    @Input() public videos: Array<Video>;
    onSelect(vid:Video){
        console.log(JSON.stringify(vid));
    }
}

【讨论】:

  • 我确实很喜欢这个视频,它也很好用youtube.com/…
  • 同样我也得到异常 例外:模板解析错误:无法绑定到“ngFor”,因为它不是已知的本机属性(“ ]*ngFor="let v of video"> {{v.id}} {{v.title}} "): PlaylistComponent@9:13
猜你喜欢
  • 2015-11-23
  • 2016-10-15
  • 2021-06-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-07
  • 1970-01-01
  • 2012-07-07
相关资源
最近更新 更多