因为我已经将@Directive创建为SelectableDirective,所以对于如何将more than one值传递给定制指令,我有点困惑.我搜索了很多,但在AngularTypescript中没有得到正确的答案.

下面是我的示例代码:

父组件为MCQComponent:

import { Component, OnInit } from '@angular/core';
import { Question } from '../question/question';
import { AppService } from '../app.service/app.service';
import { SelectableDirective } from '../selectable.directive/selectable.directive';
import { ResultComponent } from '../result-component/result.component';

@Component({
    selector: 'mcq-component',
    template: "
         .....
        <div *ngIf = 'isQuestionView'>
            <ul>
                <li *ngFor = 'let opt of currentQuestion.options' 
                    [selectable] = 'opt'
                    (selectedOption) = 'onOptionSelection($event)'>
                    {{opt.option}}
                </li>
            </ul>
            .....
        </div>

    "
    providers: [AppService],
    directives: [SelectableDirective, ResultComponent]
})
export class MCQComponent implements OnInit{
    private currentIndex:any = 0;
    private currentQuestion:Question = new Question();
    private questionList:Array<Question> = [];
    ....
    constructor(private appService: AppService){}
    ....
}

这是一个具有自定义指令[selectable]的父组件,该指令接受一个名为opt的参数.

以下是本指令的代码:

import { Directive, HostListener, ElementRef, Input, Output, EventEmitter } from '@angular/core'
import { Question } from '../question/question';

@Directive({
    selector: '[selectable]'
})
export class SelectableDirective{
    private el: HTMLElement;
    @Input('selectable') option:any;

    ...
}

所以这里我想通过more parameters from parent分,我该如何实现这一点?

推荐答案

Documentation

与组件一样,可以添加尽可能多的指令属性绑定

HighlightDirective添加一个名为defaultColor的输入属性:

@Input() defaultColor: string;

加成

<p [myHighlight]="color" defaultColor="violet">
  Highlight me too!
</p>

Angular知道defaultColor绑定属于HighlightDirective绑定,因为您向@Input公开了它

不管怎样,@Input装饰师告诉Angular这个属性是

For your example

With many parameters

使用@Input() decorator将属性添加到Directive类中

@Directive({
    selector: '[selectable]'
})
export class SelectableDirective{
    private el: HTMLElement;

    @Input('selectable') option:any;   
    @Input('first') f;
    @Input('second') s;

    ...
}

在模板中,将绑定属性传递给li元素

<li *ngFor = 'let opt of currentQuestion.options' 
    [selectable] = 'opt' 
    [first]='YourParameterHere'
    [second]='YourParameterHere'
    (selectedOption) = 'onOptionSelection($event)'>
    {{opt.option}}
</li>

li元素上,我们有一个名为selectable的指令.在selectable中,我们有两个@Input()f的名字是firsts的名字是second.我们已经将这两个应用于li个名为[first][second]的属性.我们的指令将在li个元素上找到这些属性,这是用@Input()decorator 为他设置的.所以selectable[first][second]将被绑定到li上的每个指令,li的属性都有这些名称.

With single parameter

@Directive({
    selector: '[selectable]'
})
export class SelectableDirective{
    private el: HTMLElement;

    @Input('selectable') option:any;   
    @Input('params') params;

    ...
}

加成

<li *ngFor = 'let opt of currentQuestion.options' 
    [selectable] = 'opt' 
    [params]='{firstParam: 1, seconParam: 2, thirdParam: 3}'
    (selectedOption) = 'onOptionSelection($event)'>
    {{opt.option}}
</li>

Typescript相关问答推荐

类型ElementTypes不可分配给类型ElementTypes.word'

通用默认值触发依赖通用约束变量的错误

如何在同一页面中使用布局和提供程序?

TypScript错误:类型{ children:ReactNode; }与动态React组件中的IntrinsicLedger类型没有共同的属性

如何准确确定此特定场景中的类型?

为什么ESLint抱怨通用对象类型?

为什么缩小封闭内部不适用于属性对象,但适用于声明的变量?

typescript抱怨值可以是未定义的,尽管类型没有定义

webpack错误:模块找不到.当使用我的其他库时,

Typescript泛型类型:按其嵌套属性映射记录列表

通过按键数组拾取对象的关键点

如何调整对象 struct 复杂的脚本函数的泛型类型?

带下拉菜单的Angular 响应式选项卡

为什么我的一个合成函数不能推断类型?

是否可以通过映射类型将函数参数约束为预定义类型?

使用RXJS获取数据的3种不同REST API

如何在TypeScrip中使用数组作为字符串的封闭列表?

基于属性值的条件类型

如何以类型安全的方式指定键的常量列表,并从该列表派生所挑选的接口?

如何提取具有索引签名的类型中定义的键