由于我创建了@Directive
作为SelectableDirective
,我有点困惑,关于如何通过多个价值的自定义指令,我已经搜索了很多,但没有得到正确的解决方案与Typescript,这里是我的示例代码:如何通过TypeScript将多个参数传递给Angular @Directives?
父组件为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){}
....
}
这是具有父组件定制指令,因为[可选]这需要一个PARAM称为选择,这里是这个指令代码:
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;
...
}
所以在这里我想通过从父组件多个参数,我该如何实现这一目标?
但我应该在父组件写? – Shree
@Shree见编辑的anwser。在'li'中,你以相同的方式传递参数 –
但是如果我有多个attr指令呢? – Shree