AngularDI不会向上遍历实际的DOM层次结构
创始人
2024-10-24 12:01:37
0

在Angular应用中,DI依赖注入是一个非常强大且广泛使用的功能。然而,使用ng-content,Angular的继承关系不一定符合我们期望的DOM结构。当这种情况发生时,Angular DI可能会遇到问题,因为它不会向上遍历实际的DOM层次结构。

要解决这个问题,我们可以使用ViewChild,它允许我们在组件中获取对子组件元素的引用。下面是一个使用ViewChild解决DI向上遍历时遇到的问题的示例:

在子组件中声明一个元素,然后在父组件中使用ViewChild获取子组件的引用,并通过子组件的实例来调用子组件中的方法:

子组件中的代码:

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

@Component({
  selector: 'app-child',
  template: `
    
` }) export class ChildComponent { logMessage() { console.log('Button clicked!'); } }

父组件中的代码:

This is the content of the child component.

import { Component, ViewChild } from '@angular/core';
import { ChildComponent } from './child.component';

@Component({
  selector: 'app-parent',
  template: `
    

This is the content of the child component.

` }) export class ParentComponent { @ViewChild('childComponent') childComponent:

相关内容

热门资讯

揭秘!we poker辅助器,... 揭秘!we poker辅助器,hhpoker哪个俱乐部靠谱,透视教程(有挂方法)1、许多玩家不知道h...
科普分享!aapoker辅助器... 科普分享!aapoker辅助器怎么用,德州局透视脚本免费版下载手机版,微扑克教程(有挂教程);支持2...
今日公布!hhpoker怎么破... 今日公布!hhpoker怎么破解,wepoker透视脚本是什么,规律教程(有挂神器);1.wepok...
交流学习经验!wepoker插... 1、交流学习经验!wepoker插件辅助,wepoker一直输的号能继续打吗,技巧教程(有挂软件)2...
实测交流!hhpoker是真的... 实测交流!hhpoker是真的还是假的,菠萝辅助器免费版的特点,安装教程(有挂方法)科技教程也叫必备...
玩家必看科普!德普之星app安... 此外,数据分析德州(德普之星app安卓版破解版)辅助神器app还具备辅助透视行为开挂功能,通过对客户...
记者揭秘!aapoker发牌逻... 记者揭秘!aapoker发牌逻辑,哈糖大菠萝软件下载,可靠技巧(有挂透视);1分钟了解详细教程(微信...
大家学习交流!wepoker线... 大家学习交流!wepoker线上大神,wpk私人辅助,科技教程(有挂方法)1、点击下载安装,微扑克w...
必备攻略!wepoker智能辅... 必备攻略!wepoker智能辅助插件,wepoker辅助透视,2025新版教程(有挂攻略);原来确实...
玩家必备教程!wepoker国... 玩家必备教程!wepoker国外版透视,wepoker怎么挂底牌,解密教程(有挂辅助);人气非常高,...