角度-可能有条件地应用指令吗?

问题描述

我需要根据aatest创建两个不同的 if(command === 'slap') { const taggedUser = msg.mentions.users.first(); if (!msg.mentions.users.size) { const mentionisembed = new Discord.MessageEmbed() .setColor('#0099ff') .setTitle('Slap') .setDescription('You cant slap nobody.') return msg.channel.send(mentionisembed); } gifSearch.random('slap').then( gifUrl =>{ const exampleEmbed = new Discord.MessageEmbed() .setColor('#0099ff') .setTitle('Slap') .setAuthor(taggedUser.username) .setDescription(`You slapped ${taggedUser.username}.`) .setImage(gifUrl) .setFooter('slapping idiots since 1972'); msg.channel.send(exampleEmbed); } ); } 。如果我的类型为ul,那么我想应用指令type。否则,将没有下拉指令。

是否可以有条件地应用该指令,从而避免重复的代码?

first

解决方法

在Angular 2+之后,无法基于条件应用指令。 因此需要重复您提到的代码。

,

只需将参数传递给您的指令(假设它是您的指令)。像这样。

<ul *dropdownMenu="type"></ul>

@Directive({ selector: '[dropdownMenu]' })
export class DropdownMenuDirective implements OnInit {
    private _type: string;
    
    constructor(
        private templateRef: TemplateRef<any>,private viewContainer: ViewContainerRef
    ) {}

    @Input()
    set dropdownMenu(type: string) {
        this._type = type
    }

    ngOnInit() {
        if (this._type === 'first') {
            this.viewContainer.clear() // to clear the view
        } else {
            this.viewContainer.createEmbeddedView(this.templateRef); // to create it
        }
    }
}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...