如何将模板变量ref传递给angular 6中的其他组件 [英] How to pass template variable ref to other component in angular 6

查看:354
本文介绍了如何将模板变量ref传递给angular 6中的其他组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将模板变量传递给当前组件中包含的其他组件,但是会出现未定义的错误。不知道我在做什么错。下面是组件html和ts文件代码。



< div * ngIf = dataLoaded> 
< div #topDiv id = topDiv> < / div >
< / div >
< scroll-content [targetElmRef] = target> < / 滚动内容 >





 @输入('  targetElmRef ')targetElmRef:any; 





我尝试过:



以上是代码片段



< pre lang =Javascript> 
< div * ngIf = dataLoaded>
< div #topDiv id =topDiv>< / div>
< / div>
< scroll-content [targetElmRef] =target>< / scroll-content>





 @输入('  targetElmRef')targetElmRef:any; 

解决方案

你真的需要发布父代码和组件代码来清楚你正在做什么,但是像:



父成分:



< div * ngIf = dataLoaded> 
< div #topDiv id = topDiv> < / div >
< / div >
< scroll-content [targetElmRef] = target> < / 滚动内容 >





使用父组件打字稿:



 get target():any {
返回targetObject。
}





儿童组件:



 @Input targetElmRef:any; 





如果你返回任何你的东西应该工作;重新打算绑定子组件确保目标已定义,并且您应该定义类型而不是使用任何,因为它会使目标更具可读性。


I am trying to pass template variable to other component included in current component but getting undefined error. Not sure what wrong I'm doing. Below is the component html and ts file code.

<div *ngIf=dataLoaded>
    <div #topDiv id="topDiv"></div>
</div> 
<scroll-content [targetElmRef]="target"></scroll-content>



@Input('targetElmRef') targetElmRef: any;



What I have tried:

above is the code snippet

<pre lang="Javascript">
<div *ngIf=dataLoaded>
    <div #topDiv id="topDiv"></div>
</div> 
<scroll-content [targetElmRef]="target"></scroll-content>



@Input('targetElmRef') targetElmRef: any;

解决方案

You would really need to post both the parent and the component code to make it clear what you're doing, but something like:

Parent component:

<div *ngIf=dataLoaded>
    <div #topDiv id="topDiv"></div>
</div> 
<scroll-content [targetElmRef]="target"></scroll-content>



With the parent component typescript:

get target() : any {
 return targetObject.
}



And on the child component:

@Input targetElmRef: any;



Should work if you return whatever you;re aiming to bind the child component to.

Make sure that target is defined, and you should probably define the types rather than use 'any' as it'll make it more readable.


这篇关于如何将模板变量ref传递给angular 6中的其他组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆