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.
Here is what my sample code is:
Parent Component as 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){} .... }
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?
Using Query parameters in Angular, we can pass optional parameters to angular routes in our applications. There are two ways we can pass query parameters in angular. Using RouterLink. We will go through few examples to understand further. Pass query parameters to Router.navigate using queryParams. Passing multiple query parameters.
The Angular adds the map all the route parameters in the ParamMap object, which can be accessed from the ActivatedRoute service The ParamMap makes it easier to work with parameters.
In this tutorial, You learn how to pass multiple arguments in the pipe in Angular pipe. Usually, pipe accepts single arguments, Multiple arguments can be passed to pipe with delimiter : separation. Suppose we have employee data coming from REST API and want to display the data in a database table.
This is done via the ActivatedRoute service from angular/router module to get the parameter value The ActivatedRoute is a service, which keeps track of the currently activated route associated with the loaded Component. The Angular adds the map all the route parameters in the ParamMap object, which can be accessed from the ActivatedRoute service
From the Documentation
As with components, you can add as many directive property bindings as you need by stringing them along in the template.
Add an input property to
HighlightDirective
calleddefaultColor
:@Input() defaultColor: string;
Markup
<p [myHighlight]="color" defaultColor="violet"> Highlight me too! </p>
Angular knows that the
defaultColor
binding belongs to theHighlightDirective
because you made it public with the@Input
decorator.Either way, the
@Input
decorator tells Angular that this property is public and available for binding by a parent component. Without@Input
, Angular refuses to bind to the property.
For your example
With many parameters
Add properties into the Directive
class with @Input()
decorator
@Directive({ selector: '[selectable]' }) export class SelectableDirective{ private el: HTMLElement; @Input('selectable') option:any; @Input('first') f; @Input('second') s; ... }
And in the template pass bound properties to your li
element
<li *ngFor = 'let opt of currentQuestion.options' [selectable] = 'opt' [first]='YourParameterHere' [second]='YourParameterHere' (selectedOption) = 'onOptionSelection($event)'> {{opt.option}} </li>
Here on the li
element we have a directive with name selectable
. In the selectable
we have two @Input()
's, f
with name first
and s
with name second
. We have applied these two on the li
properties with name [first]
and [second]
. And our directive will find these properties on that li
element, which are set for him with @Input()
decorator. So selectable
, [first]
and [second]
will be bound to every directive on li
, which has property with these names.
With single parameter
@Directive({ selector: '[selectable]' }) export class SelectableDirective{ private el: HTMLElement; @Input('selectable') option:any; @Input('params') params; ... }
Markup
<li *ngFor = 'let opt of currentQuestion.options' [selectable] = 'opt' [params]='{firstParam: 1, seconParam: 2, thirdParam: 3}' (selectedOption) = 'onOptionSelection($event)'> {{opt.option}} </li>
to pass many options you can pass a object to a @Input decorator with custom data in a single line.
In the template
<li *ngFor = 'let opt of currentQuestion.options' [selectable] = 'opt' [myOptions] ="{first: opt.val1, second: opt.val2}" // these are your multiple parameters (selectedOption) = 'onOptionSelection($event)' > {{opt.option}} </li>
so in Directive class
@Directive({ selector: '[selectable]' }) export class SelectableDirective{ private el: HTMLElement; @Input('selectable') option:any; @Input('myOptions') data; //do something with data.first ... // do something with data.second }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With