Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

viewContainerRef实例获得的方法? #5

Open
wszgrcy opened this issue Sep 16, 2018 · 5 comments
Open

viewContainerRef实例获得的方法? #5

wszgrcy opened this issue Sep 16, 2018 · 5 comments
Labels
已解决 参与者已经有了共识,愿意参与的可继续提问 讨论 欢迎大家继续参与讨论,征集各方面的观点

Comments

@wszgrcy
Copy link
Collaborator

wszgrcy commented Sep 16, 2018

目前我知道的有两种,

  1. 依靠依赖注入,获得当前组件的实例.
  2. 依靠@ViewChild('',{read:ViewContainerRef}) 获得指定元素的实例
    那么有没有可以将普通的html元素转化成ViewContainerRef实例的方法?
    看了相关的api,属性和方法都没有找到相关解决方法.只有实例->元素,没有元素->实例,所以来问下有这样的手段可以转化吗?
@asnowwolf asnowwolf added the 讨论 欢迎大家继续参与讨论,征集各方面的观点 label Sep 17, 2018
@asnowwolf
Copy link
Contributor

ViewContainerRef 是来自 Angular 组件或指令的,普通的 HTML 元素如果没有被 Angular 强化过,那么自然就不会有这类实例了。

@wszgrcy
Copy link
Collaborator Author

wszgrcy commented Sep 17, 2018

@asnowwolf 如果是想获得父组件的实例,不通过通信的方式能一方面获取到吗

@asnowwolf
Copy link
Contributor

依赖注入并 SkipSelf 就是标准方式了。这能满足你的需求不?

@wszgrcy
Copy link
Collaborator Author

wszgrcy commented Sep 17, 2018

@asnowwolf ..那还是需要通信...通过另一种方式解决了(父组件不是我写的,所以才问非通信方式.),不得不说模拟shadow dom的方式真强大,把元素移动到组件外面,css样式都能生效,之前以为没什么优点呢

@wszgrcy wszgrcy closed this as completed Sep 17, 2018
@asnowwolf
Copy link
Contributor

建议不要关闭问题,以便别人搜索它。我可以标上已解决。

要想用注入的方式通信并不要求父组件也是你写的,任何库中的父组件都可以注入进来,只要有它的类引用即可。

模拟 Shadow DOM 的方式是可以生效,但是离原生 Shadow DOM 的行为太远了,并不是很好的解决方案。

@wszgrcy wszgrcy reopened this Sep 17, 2018
@asnowwolf asnowwolf added the 已解决 参与者已经有了共识,愿意参与的可继续提问 label Sep 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
已解决 参与者已经有了共识,愿意参与的可继续提问 讨论 欢迎大家继续参与讨论,征集各方面的观点
Projects
None yet
Development

No branches or pull requests

2 participants