admin管理员组文章数量:1025557
I have a hack to implement a select box with dynamic set and by double click to have this selection box with a indecently input box. The added new options will not double (by a Set) be sorted and at least available for the selection box options.
the code will be:
import { Component, signal } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import {FormsModule} from '@angular/forms';
@Component({
selector: 'app-root',
standalone: true,
imports: [
FormsModule
],
template: `
<h1>Selectbox with Input Option</h1>
<p> select / input-box will toggle by double click </p>
<p> input value will be take over by leaving the input box </p>
@if (!isInput()){
<select
(dblclick)="isInput.set(!isInput())"
[ngModel]="selectItem()"
(change)="onSelectChange($any($event.target).value)">
>
@for (item of OptionValue(); track item) {
<option >
{{ item }}
</option>
}
</select>} @else {
<input
(dblclick)="isInput.set(!isInput())"
[ngModel]="selectItem()"
(focusout)="onSelectChange($any($event.target).value)">
}
<hr>
<p>show selected item: {{selectItem()}}</p>
<hr>
`,
})
export class App {
name = 'Select with Input';
public selectItem=signal('A');
public OptionValue =signal(new Set<string>(['A','B','C']));
protected isInput=signal(false)
onSelectChange(item:string){
this.selectItem.set(item);
// this.OptionValue.update(items => { return[ ...items, item];})
this.OptionValue().add(item);
const OptionValueAy = Array.from(this.OptionValue()).sort();
this.OptionValue().clear()
OptionValueAy .forEach(item => this.OptionValue().add(item))
}
} //end of class
For more details, please refer to stackblitz:
.ts
However, I am sure, that hack could be optimized. I looking fromward to your kind comments.
I have a hack to implement a select box with dynamic set and by double click to have this selection box with a indecently input box. The added new options will not double (by a Set) be sorted and at least available for the selection box options.
the code will be:
import { Component, signal } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import {FormsModule} from '@angular/forms';
@Component({
selector: 'app-root',
standalone: true,
imports: [
FormsModule
],
template: `
<h1>Selectbox with Input Option</h1>
<p> select / input-box will toggle by double click </p>
<p> input value will be take over by leaving the input box </p>
@if (!isInput()){
<select
(dblclick)="isInput.set(!isInput())"
[ngModel]="selectItem()"
(change)="onSelectChange($any($event.target).value)">
>
@for (item of OptionValue(); track item) {
<option >
{{ item }}
</option>
}
</select>} @else {
<input
(dblclick)="isInput.set(!isInput())"
[ngModel]="selectItem()"
(focusout)="onSelectChange($any($event.target).value)">
}
<hr>
<p>show selected item: {{selectItem()}}</p>
<hr>
`,
})
export class App {
name = 'Select with Input';
public selectItem=signal('A');
public OptionValue =signal(new Set<string>(['A','B','C']));
protected isInput=signal(false)
onSelectChange(item:string){
this.selectItem.set(item);
// this.OptionValue.update(items => { return[ ...items, item];})
this.OptionValue().add(item);
const OptionValueAy = Array.from(this.OptionValue()).sort();
this.OptionValue().clear()
OptionValueAy .forEach(item => this.OptionValue().add(item))
}
} //end of class
For more details, please refer to stackblitz:
.ts
However, I am sure, that hack could be optimized. I looking fromward to your kind comments.
本文标签: htmlAngular 18 hack for ltselectgt with ltinputgt in Signal() way to be optimizedStack Overflow
版权声明:本文标题:html - Angular 18 hack for <select> with <input> in Signal() way to be optimized - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745630888a2160157.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论