我在这里try 遵循基本的Angular 2教程:

https://angular.io/docs/js/latest/guide/displaying-data.html

我可以用下面的代码让ANGLE应用程序加载并显示我的名字:

import { Component, View, bootstrap } from 'angular2/angular2';

@Component({
    selector: "my-app"
})

class AppComponent {
    myName: string;
    names: Array<string>;
    constructor() {
        this.myName = "Neil";

    }

}
bootstrap(AppComponent);

但是,当我try 添加字符串数组并try 使用ng for显示它们时,它会抛出以下错误:

Can't bind to 'ng-forOf' since it isn't a known native property ("
    <p>Friends:</p>
    <ul>
        <li [ERROR ->]*ng-for="#name of names">
        {{ name }}
        </li>
"): AppComponent@4:16
Property binding ng-forOf not used by any directive on an embedded template ("
    <p>Friends:</p>
    <ul>
        [ERROR ->]<li *ng-for="#name of names">
        {{ name }}
        </li>
"): AppComponent@4:12

以下是代码:

import { Component, View, bootstrap } from 'angular2/angular2';

@Component({
    selector: "my-app"
})

@View({
    template: `
        <p>My name: {{ myName }}</p>
        <p>Friends:</p>
        <ul>
            <li *ng-for="#name of names">
                {{ name }}
            </li>
        </ul>
    `,
    directives: [ NgFor ]
})

class AppComponent {
    myName: string;
    names: Array<string>;
    constructor() {
        this.myName = "Neil";
        this.names = ["Tom", "Dick", "Harry"];
    }

}
bootstrap(AppComponent);

我遗漏了什么?

推荐答案

如果使用alpha 52,请查看GitHub repo中的CHANGELOG.md.他们将模板改为区分大小写,即ngFor而不是ng-for(与所有其他指令类似)

虽然没有更改像<router-outlet>这样的元素名称以保持与自定义元素规范的兼容性,但这需要在自定义元素的标记名中加上破折号.

In >= RC.5 (and final) ngFor and similar directives are not ambient by default. They need to be provided explicitly like

@NgModule({
  imports: [CommonModule],

或者,如果您不介意将模块锁定为仅限浏览器使用

@NgModule({
  imports: [BrowserModule],

BrowserModule出口CommonModule,就像WorkerAppModule出口一样.

Update

BrowserModule应该被导入到应用程序模块中,而在其他模块中CommonModule应该被导入.

Angular相关问答推荐

日语字符不受角形约束控制

具有多重签名的Angular Mocking Service

Ngrx Select 器返回部分对象

Observable转换为Observable

ngrx 效果等到从初始值以外的其他状态收到响应

Angular AuthGuard:不推荐使用 CanActivate 和 CanActivateChild.如何更换它们?

console.log 返回 api 数据但 ERROR TypeError: Cannot read properties of undefined reading 'name'

我想将一个对象传递给一个子组件.我做了一切,但没有任何效果. (Angular )

为什么 Angular2 (click) 事件没有在

如何在两个模块之间共享服务 - @NgModule 在Angular 而不是在组件之间?

Angular ngFor 索引从 1 开始 循环

找不到模块'webpack'

带有嵌套表单数组的 Angular react式表单

获取从(keypress)angular2按下的键

Failed to execute 'setAttribute' on 'Element': ']' is not a valid attribute name

Angular 2 filter/search 列表

Angular 2 router.navigate

输入更改时Angular2动态输入字段失go 焦点

NPM 脚本 start退出,但未指示 Angular CLI 正在侦听请求

找不到@angular/common/http模块