简体   繁体   English

如何使用 kebab case 选择器将第二个输入传递给指令?

[英]How to pass in a second input to a directive with a kebab case selector?

Say I have a directive that has a selector of selector: '[example-directive]' .假设我有一个带有选择器selector: '[example-directive]' Is there a way to pass in a second input to the directive?有没有办法将第二个输入传递给指令? I've been able to succeed by changing the selector to selector: '[exampleDirective]' and prefixing the input with exampleDirective, but I'm wondering if there's a way to do it in kebab case.通过将选择器更改为selector: '[exampleDirective]'并在输入前加上 exampleDirective,我已经成功了,但我想知道是否有办法在 kebab 案例中做到这一点。

This is roughly what the directive looks like:这大致是指令的样子:

@Directive({
    selector: '[example-directive]',
})
export class ExampleDirective implements OnChanges {
    @Input('example-directive') input1: string;

    @Input() exampleDirectiveInput2: string;

I've tried to add an alias to the second input with no luck:我试图在没有运气的情况下向第二个输入添加别名:

@Input('input2') exampleDirectiveInput2: string;

html: <div *example-directive="'value1'; input2: 'value2'"></div> html: <div *example-directive="'value1'; input2: 'value2'"></div>

If you would like to pass multiple parameter into the same structural directive you could wrap them into an object.如果你想将多个参数传递到同一个结构指令中,你可以将它们包装到一个 object 中。

But you can also define multiple inputs, but then you need to pass it like two directives.但是你也可以定义多个输入,但是你需要像两个指令一样传递它。

<div 
    example-directive="'value1'"
    input2="'value2'"
></div>

Also check out this question: https://stackoverflow.com/a/38843554/5247706还要检查这个问题: https://stackoverflow.com/a/38843554/5247706

Quick answer: instead of "exampleDirectiveInput2", try example-directiveInput2 .快速回答:尝试example-directiveInput2而不是“exampleDirectiveInput2”。 If you want to learn more, please scroll down.如果您想了解更多,请向下滚动。

Your case is the same as the ngIf from angular.您的情况与ngIf中的 ngIf 相同。
The syntax of structure directive is called microsyntax . structure directive的语法称为microsyntax语法。 Here is a good explanation article (don't forget to read the reference links at the end of the article too!) https://www.lucaspaganini.com/academy/angular-structural-directives-and-their-microsyntax这里有一篇很好的解释文章(别忘了阅读文章末尾的参考链接!) https://www.lucaspaganini.com/academy/angular-structural-directives-and-their-microsyntax 在此处输入图像描述

I know I should provide more information, however, it will become a very long answer, plus, the article gives a very detailed answer already, so no need to rewrite it here.我知道我应该提供更多信息,但是,这将成为一个很长的答案,而且文章已经给出了非常详细的答案,因此无需在这里重写。

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

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