【问题标题】:Angular 2: Can't bind to x since it isn't a known native property [duplicate]Angular 2:无法绑定到 x,因为它不是已知的本机属性 [重复]
【发布时间】:2023-03-07 18:26:02
【问题描述】:

在 Angular 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' because it is not an known native property » 和 «Can't bind to 'NgIf' because it is '不是已知的原生属性»。

我正在使用 beta 8。

【问题讨论】:

标签: angular angular2-template


【解决方案1】:

一般来说,can't bind to xxx since it isn't a known native property 错误发生在您尝试使用属性指令或尝试设置属性绑定时,您的 HTML 中有错字。

常见的例子是当你错过 * 或 # 或 let 或使用 in 而不是 of 与 Angular 内置结构指令:

<div  ngIf="..."                 // should be *ngIf
<div  ngFor="..."                // should be *ngFor="..."
<div *ngFor="let item in items"  // should be "let item of items"
<div *ngFor="item of items"      // should be "let item of items"

拼写错误或大小写错误也会产生问题::

<div *ngFer="..."
<div *NgFor="..."

另一个原因是,如果您指定了 DOM 元素或组件上不存在的属性:

<div [prop1]="..."       // prop1 isn't a valid DOM property for a div
<my-comp [answr]="..."   // typo, should be [answer]

对于带有内置 Angular 指令的拼写错误,由于拼写错误与任何内置指令选择器都不匹配,Angular 尝试设置绑定到 DOM 元素的属性(上面的 div示例)带有拼写错误的名称。这会失败,因为 div 没有本机 ngIf 或 ngFer 或 prop1 DOM 属性。

--

对于属性(不是属性)你需要使用属性绑定,例如svg的height属性:&lt;svg [attr.height]="myHeightProp"&gt;

【讨论】:

  • 有没有办法找到所有元素的所有原生 DOM 属性?当我尝试绑定到 svg 元素的 height 属性时,我遇到了同样的问题。
  • 我不知道有任何这样的列表,尤其是考虑到,例如 (according to MDN) DOM 元素继承自 Node 和 EventTarget,并实现了 ParentNode、ChildNode、NonDocumentTypeChildNode 和 Animatable。每一个都可以有自己的属性。要查找属性,我通常使用 MDN 或使用 Chrome 开发工具(元素,然后是属性选项卡)。在您的情况下,height 是 svg 元素的属性,而不是属性,因此您需要使用属性绑定:&lt;svg [attr.height]="myHeightProp"&gt;。
  • 我遇到了这个错误,因为我忘记在我的组件的组件声明中将指令添加到指令数组中。
  • 对我来说,我忘记在我的模块的Exports 中添加我的组件
【解决方案2】:

尝试使用[(ngModel)] 而不是*NgModel 和*ngIf 而不是*NgIf

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

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

【讨论】:

  • 仅供参考...我不确定,但由于 beta-8 没有必要导入 FORM_DIRECTIVES &amp; COMMON_DIRECTIVES
  • COMMON_DIRECTIVES 和 CORE_DIRECTIVES 是还是不同的东西?
  • 大写版本指类名,小写版本指实例。使用实例。
猜你喜欢
  • 2017-03-18
  • 2017-04-06
  • 2016-10-16
  • 2016-09-05
  • 2016-06-02
  • 1970-01-01
  • 2016-03-17
相关资源
最近更新 更多