host和@HostBinding导致不同的行为 [英] host and @HostBinding leads to different behavior

查看:78
本文介绍了host和@HostBinding导致不同的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够以角度运行一些伪验证".这意味着我们的客户想要显示为警告的验证",但在角度上仍然有效.

I need to able to run some "pseudo-validation" in angular. That means our customer wants "validation" that displays as warning but it is still valid in angular terms.

为此,我使用angular的形式验证(因为这似乎是正确的方法).

For that I use angular's form validation (because it seems the proper way to go).

我创建了一条指令来更新其主机的CSS类,类似于

I created a directive which updates the css class of its host, similar to what angular itself does

在尝试了不同的方法之后(总是使用 @HostBinding ),我注意到使用 @HostBinding 时永远不会添加该类,但是当使用指令的<代码>主机

After spending ours of trying out different things (always with @HostBinding) I noticed the class never gets added when using @HostBinding but works successfully when using the directive's host

请参阅stackblitz-> https://stackblitz.com/edit/angular-keky2p?file=src%2Fapp%2Fapp.component.ts

See the stackblitz -> https://stackblitz.com/edit/angular-keky2p?file=src%2Fapp%2Fapp.component.ts

目标是渲染橙色作为输入的背景.如果使用 host ,则可以使用,如果使用 @HostBinding ,则不能使用.

the goal is to render orange as background of the input. if you use host it works, if you use @HostBinding it doesn't.

函数 asWarning 只是使任何提供的验证器始终返回NULL(有效)

the function asWarning just makes any provided validator always return NULL (valid)

它们应该等效,但似乎存在一些差异.我做错什么了吗?还是不一样?

They should be equivalent, yet there seems to be some differences. Am I doing something wrong or are they not equivalent?

推荐答案

无需引入字段.
您可以将 @HostBinding(...)添加到 get displayWarning(){...

There is no need to introduce a field.
You can add @HostBinding(...) to get displayWarning() {...

这篇关于host和@HostBinding导致不同的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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