如何在其他元素上使用自定义指令创建的引用?

发布于 2025-01-21 11:32:54 字数 2391 浏览 3 评论 0 原文

我正在尝试重现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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

因为他们在声明Mat-autocomplete组件时使用了exportas,

代码在这里:

@Component({
  selector: 'mat-autocomplete',
  templateUrl: 'autocomplete.html',
  styleUrls: ['autocomplete.css'],
  encapsulation: ViewEncapsulation.None,
  changeDetection: ChangeDetectionStrategy.OnPush,
  exportAs: 'matAutocomplete', <-- this line
  inputs: ['disableRipple'],
  providers: [...],
})
export class MatAutocomplete extends _MatAutocompleteBase {
  ...
}

通过Exportas的定义,定义了可以在模板中使用的名称将此指令分配给变量。 在这里

您还声明exportas exportas:'appautocompletecustom'。但是它在内部自动填充。这就是为什么添加指令时,

<app-autocomplete #auto="appAutocompleteCustom" appAutocompleteCustom></app-autocomplete>

错误已经消失了

Because they used exportAs when they declare mat-autocomplete component

The code is here:

@Component({
  selector: 'mat-autocomplete',
  templateUrl: 'autocomplete.html',
  styleUrls: ['autocomplete.css'],
  encapsulation: ViewEncapsulation.None,
  changeDetection: ChangeDetectionStrategy.OnPush,
  exportAs: 'matAutocomplete', <-- this line
  inputs: ['disableRipple'],
  providers: [...],
})
export class MatAutocomplete extends _MatAutocompleteBase {
  ...
}

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 inside AutocompleteCustomDirective. That why when you add the directive

<app-autocomplete #auto="appAutocompleteCustom" appAutocompleteCustom></app-autocomplete>

the error is gone

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文