我正在尝试重现Angular Material的自动完成组件的第一个示例
https://material.io/compol.io/components/autococtlete/autococtelete/autocoldete/overview
<form class="example-form">
<mat-form-field class="example-full-width" appearance="fill">
<mat-label>Number</mat-label>
<input type="text"
placeholder="Pick one"
aria-label="Number"
matInput
[formControl]="myControl"
[matAutocomplete]="auto">
<mat-autocomplete #auto="matAutocomplete">
<mat-option *ngFor="let option of options" [value]="option">
{{option}}
</mat-option>
</mat-autocomplete>
</mat-form-field>
</form>
我需要拥有可重复使用的组件,但我找不到他们如何做到这一点。
因此,他们有一个指令称为 matautocomplete
和一个自定义组件 mat-autocomplete
。
首先,他们使用#uato =“ matautocomplete”
在自定义组件上创建参考。创建了参考变量后,他们将该引用发送到 matautocomplete
指令,并在输入 [matautocomplete] =“ auto”
上使用此代码。
当我尝试重现同一件事时,我会制作一个自定义自动complete指令和一个自定义自动完成组件。
如您所见,它们都是空的 - 因此,自定义创建的组件中的HTML中没有什么。
@Component({
selector: 'app-autocomplete',
templateUrl: './autocomplete.component.html',
styleUrls: ['./autocomplete.component.css']
})
export class AutocompleteComponent implements OnInit {
}
tooltip Directive
import { Directive, Input } from '@angular/core';
@Directive({
selector: '[appAutocompleteCustom]',
exportAs: 'appAutocompleteCustom'
})
export class AutocompleteCustomDirective {
@Input() appAutocompleteCustom;
constructor() { }
}
之后,在 app.component.ts
文件中,当我尝试类似于它们的同一件事时,
<input type="text"
[appAutocompleteCustom]="auto"
/>
<app-autocomplete #auto="appAutocompleteCustom" ></app-autocomplete>
我会获得错误
没有“ exportas”设置的指令到“ appautocompletecustom”
。
因此,为防止此错误发生,我需要添加关键字 appautocompletecustom
,因此我在创建参考的元素上的指令名称。
<app-autocomplete #auto="appAutocompleteCustom" appAutocompleteCustom></app-autocomplete>
此后,错误消失了。我的问题是他们如何做到这一点?当他们不在 mat-autocomplete
组件上附加指令关键字时,他们如何没有错误
I am trying to reproduce the first example on Angular Material's autocompleted component
https://material.angular.io/components/autocomplete/overview
<form class="example-form">
<mat-form-field class="example-full-width" appearance="fill">
<mat-label>Number</mat-label>
<input type="text"
placeholder="Pick one"
aria-label="Number"
matInput
[formControl]="myControl"
[matAutocomplete]="auto">
<mat-autocomplete #auto="matAutocomplete">
<mat-option *ngFor="let option of options" [value]="option">
{{option}}
</mat-option>
</mat-autocomplete>
</mat-form-field>
</form>
I need to have reusable component as they have but I can't find a way how they do that.
So they have a directive called matAutocomplete
and one custom component mat-autocomplete
.
First, they create reference on the custom component with #auto="matAutocomplete"
. Once the reference variable is created they send that reference to the matAutocomplete
directive with this code on the input [matAutocomplete]="auto"
.
When I try to reproduce the same thing, I make for example one custom autcomplete directive and one custom autocomplete component.
As you can see they are both empty - so there is nothing in the HTML in the custom-created component.
@Component({
selector: 'app-autocomplete',
templateUrl: './autocomplete.component.html',
styleUrls: ['./autocomplete.component.css']
})
export class AutocompleteComponent implements OnInit {
}
TOOLTIP DIRECTIVE
import { Directive, Input } from '@angular/core';
@Directive({
selector: '[appAutocompleteCustom]',
exportAs: 'appAutocompleteCustom'
})
export class AutocompleteCustomDirective {
@Input() appAutocompleteCustom;
constructor() { }
}
After that in the app.component.ts
file when i try the same thing like they do
<input type="text"
[appAutocompleteCustom]="auto"
/>
<app-autocomplete #auto="appAutocompleteCustom" ></app-autocomplete>
i get error
There is no directive with "exportAs" set to "appAutocompleteCustom"
.
So to prevent this error to happen i need to add the keyword appAutocompleteCustom
so the name of the directive on the element where i created the reference.
<app-autocomplete #auto="appAutocompleteCustom" appAutocompleteCustom></app-autocomplete>
after this the error is gone. My question is how they do this ? How they are not having the error when they are not attaching the directive keyword on their mat-autocomplete
component
发布评论
评论(1)
因为他们在声明Mat-autocomplete组件时使用了exportas,
代码在这里:
通过Exportas的定义,
定义了可以在模板中使用的名称将此指令分配给变量。
在这里。您还声明exportas
exportas:'appautocompletecustom'
。但是它在内部自动填充
。这就是为什么添加指令时,错误已经消失了
Because they used exportAs when they declare mat-autocomplete component
The code is here:
By the definition of exportAs,
Defines the name that can be used in the template to assign this directive to a variable.
here.You also declare exportAs
exportAs: 'appAutocompleteCustom'
. But it's insideAutocompleteCustomDirective
. That why when you add the directivethe error is gone