【发布时间】:2016-12-15 01:47:01
【问题描述】:
由于我将@Directive 创建为SelectableDirective,我有点困惑,关于如何将多个 值传递给自定义指令。我进行了很多搜索,但没有在 Angular 中使用 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 的参数。
这是该指令的代码:
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;
...
}
所以这里我想从父组件传递更多参数,我该如何实现呢?
【问题讨论】:
标签: javascript angular typescript angular6