最佳答案
因为我已经将 @Directive
创建为 SelectableDirective
,所以对于如何将 不止一个值传递给自定义指令,我有点困惑。我已经找了很多,但没有得到适当的解决方案在 角度与 打印稿。
下面是我的示例代码:
作为 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){}
....
}
这是一个具有自定义指令 [可选]的父组件,该指令接受一个名为 选吧的参数。
下面是这个指令的代码:
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;
...
}
所以这里我想传递 来自父级的更多参数组件,我如何实现这一点?