如何使用TypeScript将多个参数传递给Angular中的@Directives(@Components)? [英] How to pass multiple parameter to @Directives (@Components) in Angular with TypeScript?
本文介绍了如何使用TypeScript将多个参数传递给Angular中的@Directives(@Components)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
由于我已将@Directive
创建为SelectableDirective
,因此我对如何将一个以上值传递给自定义指令有点困惑.我进行了很多搜索,但在 Angular 中使用 Typescript 找不到正确的解决方案.
Since I've created @Directive
as SelectableDirective
, I'm little bit confused, about how to pass more than one value to the custom directive. I have searched a lot but didn't get proper solution in Angular with Typescript.
这是我的示例代码:
父组件为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 的参数.
This is a parent component having custom directive [selectable] which takes one param called opt.
以下是此伪指令的代码:
Here is the code for this directive:
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;
...
}
所以在这里我想从父组件传递更多参数,我该如何实现呢?
So here I want to pass more parameters from parent component, how do I achieve this?
推荐答案
查看全文