在角2分量中,我有authbox.component.ts

import {Component} from 'angular2/core';
import {COMMON_DIRECTIVES} from 'angular2/common';
import {Credentials} from './credentials'
@Component({
    selector: 'authbox',
    template: `<div>
       <div class="login-panel" *NgIf="!IsLogged">
            <input type="text" *NgModel="credentials.name" />
            <input type="password" *NgModel="credentials.password" />
            <button type="button" (click)="signIn(credentials)">→| Sign In</button>
        </div>
        <div class="logged-panel" *NgIf="IsLogged">
            <span>{nickname}</span>&nbsp;&nbsp; <button type="button" (click)="signOut()">|→ Sign out</button>
        </div>
    </div>`,
    directives: [COMMON_DIRECTIVES]
})


export class AuthBoxComponent {

    private _isLogged: boolean;

    get IsLogged(): boolean {
        return this._isLogged
    }
    set IsLogged(value: boolean) {
        this._isLogged = value;
    }

    public credentials: Credentials;

}

在浏览器中,我得到了错误«Can't bind to 'NgModel' since it isn't a known native property»和«Can't bind to 'NgIf' since it isn't a known native property».

我用的是beta 8.

推荐答案

试着用[(ngModel)]而不是*NgModel,用*ngIf而不是*NgIf

<span>{{nickname}}</span>&nbsp;&nbsp; <button type="button" (click)="signOut()">|→ Sign out</button>

export class AuthBoxComponent {
    nickname="guest";
    ...
}

Angular相关问答推荐

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

iOS Mobile Safari - HTML5视频覆盖一切

导致无限请求的Angular HttpInterceptor和HttpClientModule

命令不起作用的初始菜单项

存储服务存储在本地存储中,但无法检索到它

路由在全局导航中总是给我/甚至使用相对路径

如何包装NGB carousel ?

弯管记忆

Angular 13 - 使用 PUT 时出现 400 错误(错误请求)

导入 AngularFirestoreModule 时出现问题:此类型参数可能需要 `extends firebase.firestore.DocumentData` 约束.

模块内的命名路由出口 - 路径匹配但内容未加载

Angular 重新渲染仅使用ngrx在表中更改的行

通过 SignalR 事件组件重定向Angular 页面后不起作用

最新版本的 Chrome ERR_TIMED_OUT

Observable .do() 运算符 (rxjs) 的用例

Springboot/Angular2 - 如何处理 HTML5 url?

如何在Angular 4中为数字管道指定语言环境千位分隔符

Angular 4 错误:没有 HttpClient 的provider提供者

为什么 *ngIf 不适用于 ng-template?

Angular 2表单验证重复密码