简体   繁体   English

Angular 2指令名称冲突 - 为什么这不会破坏?

[英]Angular 2 directive name conflict — why did this not break?

I was looking into some code I had already written and got working, and I noticed this: 我正在研究一些我已编写并开始工作的代码,我注意到了这一点:

@Component({
  selector: 'app-doc-edit',
  templateUrl: './doc-edit.component.html',
  styleUrls: ['./doc-edit.component.css']
})
export class DocEditComponent implements OnInit {
   ...
  @Input() id: number;

the component was invoked like this: 该组件被调用如下:

  <app-doc-edit [id]="path.path.id">
  </app-doc-edit>

Why does the id input to the component not cause problems? 为什么输入组件的id不会导致问题? My understanding is that user defined Angular 2 directives occupied the same name space as the standard HTML definitions but this action worked fine. 我的理解是,用户定义的Angular 2指令占用了与标准HTML定义相同的名称空间,但此操作运行正常。

Of course I am going to fix this (WebStorm refactor/rename to the rescue) but now I think my understanding of Angular 2 works is faulty. 当然我要解决这个问题(WebStorm重构/重命名以拯救)但现在我认为我对Angular 2工作的理解是错误的。 Can anyone explain? 谁能解释一下?

Angular looks first to see if the name is a property of a known directive. Angular首先查看名称是否是已知指令的属性。 Technically, angular is matching the name to a directive input, one of the property names listed in the directive's inputs array or a property decorated with @Input() . 从技术上讲,angular将名称与指令输入,指令的inputs数组中列出的属性名称之一或使用@Input()修饰的属性进行@Input()

Only then if such property was not found in boundDirectivePropNames it compares property with the standart HTML definitions 只有这样,如果在boundDirectivePropNamesboundDirectivePropNames这样的属性,它会将属性与标准HTML定义进行比较

private _createElementPropertyAsts(
    elementName: string, props: BoundProperty[],
    boundDirectivePropNames: Set < string > ): BoundElementPropertyAst[] {
    const boundElementProps: BoundElementPropertyAst[] = [];

    props.forEach((prop: BoundProperty) => {
        if (!prop.isLiteral && !boundDirectivePropNames.has(prop.name)) { // don't add if exists in directive
            boundElementProps.push(this._bindingParser.createElementPropertyAst(elementName, prop));
        }
    });
    return this._checkPropertiesInSchema(elementName, boundElementProps); // check in standart HTML definitions
}

https://github.com/angular/angular/blob/4.1.3/packages/compiler/src/template_parser/template_parser.ts#L641-L646 https://github.com/angular/angular/blob/4.1.3/packages/compiler/src/template_parser/template_parser.ts#L641-L646

See also 也可以看看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM