由於我創建了@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