I am using a Bootstrap dropdown like this:
<ul class="dropdown-menu">
<li>
<a class="dropdown-item" href="#" (click)="getMyValue('myvalue')">My Value</a>
</li>
<li>
<a class="dropdown-item" href="#" (click)="getMyValue('someothervalue')">Some Other Label</a>
</li>
</ul>
and the function looks like this:
getMyValue(todo: string) {
console.log("selected value is:", todo);
console.log("selected text is:", ???);
}
Instead of parsing the text as the second parameter like "My Value 01", "02" etc.
Is there any way I can omit this and use it smart so I don’t need to write the same things several times?
2
Answers
You can use
@for
for running the loop and using@if with as
syntax, to create the display label and reuse it for both the function and the label!Angular Control Flow Syntax
Stackblitz Demo
You can pass the
event
to your method.And retrieve the label with
event.target.value
.Alternatively, you may consider working with an array to generate the HTML elements.
So you can pass each
item
element to the method during click event.Demo @ StackBlitz