简体   繁体   English

Angular - 带有或不带值输入的指令

[英]Angular - directive with input with or without value

I'm refactoring a directive which adds some sort of a tooltip over the form controls on mouse hover/focus.我正在重构一个指令,该指令在鼠标悬停/焦点的表单控件上添加某种工具提示。 It's simple to use.使用起来很简单。 Just add it to the element and define its text.只需将其添加到元素并定义其文本。 The input you use for the text also defines the tooltip's styling您用于文本的输入还定义了工具提示的样式

<!-- Creates red tooltip -->
<input matInput type="text" libMessage [libError]="'Error Text'" />
<!-- Creates blue tooltip -->
<input matInput type="text" libMessage [libInfo]="'Info Text'" />
<!-- Creates yellow tooltip -->
<input matInput type="text" libMessage [libWarning]="'Warning Text'" />

We received a request to extend it with option to display not just the text, but also a custom component, which provides better options for styling.我们收到了扩展它的请求,不仅可以显示文本,还可以显示自定义组件,这为样式提供了更好的选择。 It works but it feels bit cumbersome.它有效,但感觉有点麻烦。 Instead of just providing a component you have to specify it's type by providing [libError] value with non-empty string.您必须通过提供带有非空字符串的[libError]值来指定它的类型,而不是仅仅提供一个组件。

<!-- Creates red tooltip from simple text -->
<input matInput type="text" libMessage [libError]="'Error Text'" />

<!-- Creates a tooltip from component with red background. Text is ignored. -->
<input matInput type="text" libMessage [libError]="'Error Text'" [libCustomComponent]="someComponent" />

I came with couple of workarounds but was wondering if it's possible to have a directive's input working in two ways.我提供了几种解决方法,但想知道是否可以让指令的输入以两种方式工作。 As the Input and the Attribute at the same time.同时作为输入和属性。 If used with value, it defines the text, if used alone, it defines style but the component has to be provided.如果与 value 一起使用,它定义文本,如果单独使用,它定义样式但必须提供组件。 Something like this.像这样的东西。

<!-- [libError] specifies the text and type -->
<input matInput type="text" libMessage [libError]="'Error Text'" />

<!-- libError defines just a style, no value is needed. -->
<input matInput type="text" libMessage libError [libCustomComponent]="someComponent" />

Is it possible to do something like this?有可能做这样的事情吗?

Yes, it is possible, but then you need to turn your libError property into a boolean instead.是的,这是可能的,但是您需要将libError属性转换为布尔值。 For a better overview, check out boolean property coercion ( this is how the CDK does it ).要获得更好的概述,请查看布尔属性强制转换( CDK 就是这样做的)。

For an usage example, we can take a look at the mat-divider .对于使用示例,我们可以查看mat-divider It uses boolean coercion to allow us to write something as simple as this:它使用布尔强制来允许我们写一些像这样简单的东西:

<mat-divider vertical />

whenever we need a vertical divider.每当我们需要垂直分隔线时。

I think you can achieve this with the directive that can accept a value using @Input and provide some default value for @Input decorator.我认为您可以使用可以使用 @Input 接受值并为 @Input 装饰器提供一些默认值的指令来实现这一点。 So once you provide an input value while adding directive to you DOM element it accept and behave with the input value otherwise if value is not provided directive uses default value that you have set for @Input.因此,一旦您在向您的 DOM 元素添加指令时提供了输入值,它就会接受并按照输入值行事,否则如果未提供值,指令将使用您为 @Input 设置的默认值。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM