我有不同選項的下拉列表。我希望將選定的選項保留在下拉列表的頂部,直到用戶更改它。現在,如果我選擇任何選項,我走出去的組件,然後再回來,下拉與第一值重置....從下拉列表中保留選定的選項
HTML
<select class="form-control-mb-12"
(change)="intSelection($event.target.value)" >
<option value="1/4">1/4</option>
<option value="1/8">1/8</option>
<option value="1/16">1/16</option>
<option value="1/32">1/32</option>
</select>
組件
import { Component, OnInit } from '@angular/core';
import {NgbModal, ModalDismissReasons} from '@ng-bootstrap/ng-bootstrap';
import { ModuladorService } from 'app/Services/modulador.service'
@Component({
selector: 'app-resumen',
templateUrl: './resumen.component.html',
styleUrls: ['./resumen.component.scss'],
providers: [ModuladorService]
})
export class ResumenComponent implements OnInit {
intSelected : string = "" ;
constructor(private _moduladorService:ModuladorService) {
this.intSelected = this._moduladorService.obtenerParametros();
}
ngOnInit() {
}
intSelection(value){
switch(value) {
case "1/4":
this.intSelected = value;
break;
case "1/8":
this.intSelected = value;
break;
case "1/16":
this.intSelected = value;
break;
case "1/32":
this.intSelected = value;
break;
}
this._moduladorService.actualizarParametros(this.intSelected);
}
服務
import { Injectable } from '@angular/core';
import { InitParam } from 'app/layout/modulador/resumen/init-param'
@Injectable()
export class ModuladorService extends InitParam {
constructor() {
super();
this.load();
}
obtenerParametros(){
var intSelected = JSON.parse(localStorage.getItem('intSelected'));
return intSelected;
}
actualizarParametros(newParam : any){
var intSelected = JSON.parse(localStorage.getItem('intSelected'));
intSelected = newParam;
localStorage.setItem('intSelected', JSON.stringify(intSelected));
}
}
初始化以服務
export class InitParam {
load(){
if(localStorage.getItem('intSelected')=== null){
console.log('No se encontraron parametros...');
var intSelected = '1/4'
localStorage.setItem('intSelected', JSON.stringify(intSelected));
}else{
console.log('Cargando parametros...');
}
}
}
您真的不需要那裏的switch語句,它什麼都不做,只需保留'''intSelection(value){this。 intSelected = value; }''' – LLL