简体   繁体   English

Angular:如何访问 ng-template 中的引用变量

[英]Angular: how do I access the reference variable which is in a ng-template

Is there a way to access #interface, which is declared inside ng-template element?有没有办法访问在 ng-template 元素中声明的#interface?

I must put the <interface-settings inside <ng-template.我必须把 <interface-settings 放在 <ng-template.

Because I would need to use the *ngTemplateOutlet.因为我需要使用 *ngTemplateOutlet。

As I could not find a solution for this, I have to add another <interface-settings #interface and not declare it inside ng-template.由于我找不到解决方案,我必须添加另一个 <interface-settings #interface,而不是在 ng-template 中声明它。

would appreciate if you know other workaround.如果您知道其他解决方法,将不胜感激。 Thanks谢谢

 //  parent.html
 <button type="button" (click)="interface.apply()"></button>

 <ng-template>
    <interface-settings #interface></interface-settings>
 <ng-template>

 //child.ts (interface-settings.ts)

apply() {
   console.log("apply");
 }

ng-template is used by Angular for templating - meaning it is not rendered in DOM unless certain conditions tell Angular to use it in the actual DOM. ng-template 被 Angular 用于模板 - 这意味着它不会在 DOM 中呈现,除非某些条件告诉 Angular 在实际 DOM 中使用它。

So when you have your component "interface-setting" inside ng-template - it is not accessible/known by your application unless:因此,当您在 ng-template 中有组件“接口设置”时 - 您的应用程序无法访问/知道它,除非:

  • the ng-template condition was applied and应用了 ng-template 条件,并且
  • your host (parent) component has a reference to it:您的主机(父)组件引用了它:

For instance, similar to your example:例如,类似于您的示例:

<button type="button" (click)="hello.sayHello()">Method Inside Hello</button>

<ng-template>
  <hello #hello></hello>
<ng-template>

The above on its own won't work as 2 conditions mentioned above are not met.由于不满足上述两个条件,上述内容本身不起作用。 To fix you need to use ViewChild to get reference to the hello component.要解决此问题,您需要使用 ViewChild 来获取对 hello 组件的引用。 Like so:像这样:

import { Component, ViewChild } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {

  @ViewChild('hello') hello;

}

Then you need to make sure the ng-template is used (whatever triggers the ng-template to be placed in DOM for your app):然后你需要确保使用了 ng-template(无论什么触发 ng-template 被放置在你的应用程序的 DOM 中):

<ng-template [ngIf]="true">
  <hello #hello></hello>
<ng-template>

Now the reference to the method inside child component will work:现在对子组件内部方法的引用将起作用:

https://stackblitz.com/edit/angular-ivy-hnmw17?file=src/app/app.component.html https://stackblitz.com/edit/angular-ivy-hnmw17?file=src/app/app.component.html

You can read more how Angular's structural directives under the hood use ng-template: https://angular.io/guide/structural-directives您可以阅读更多 Angular 的结构指令如何使用 ng-template: https://angular.io/guide/structural-directives

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

相关问题 如何限制 ngModel 更改对 angular7 中其他内部 ng-template 的影响? - How to restrict ngModel changes effect to other inside ng-template in angular7? Angular 7 - 嵌套<ng-template>标签不起作用 - Angular 7 - Nested <ng-template> label doesn't work 在动态组件列表上带有ng-template的cdkDropList不起作用 - cdkDropList with ng-template on dynamic component list do'snt work 我如何使用 ng-template 和触发它的操作来测试模式的元素? - How can I test a modal's elements with ng-template and the action that triggers it? 如何使用ng-for ngif来访问ng-container中的变量? - How do I access a variable inside a ng-container with an ng-for for my ngif? ng-template 中的 mat-autocomplete - mat-autocomplete inside an ng-template 如何访问 Angular 订阅中的变量 - How to access variable which is inside subscribe in Angular 从一个组件传递ng-template到另一个组件内部ng-select - Pass ng-template From A Component To ng-select Inside Another Component 如何动态访问 angular.html 中定义的模板引用变量(符号:#) - how to dynamically access template reference variables (symbol: #) defined in the angular.html 如何在 Angular 组件中创建 HtmlElement 引用? - How do I create an HtmlElement Reference in an Angular component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM