选定选项上的角度2

时间:2017-05-02 08:21:44

标签: angular typescript

我有HTML

的代码
<select [(ngModel)]="empfile.storeno"  class="form-control">
    <option *ngFor="let p of concept" [ngValue]="p.MAP_Code">{{p.MAP_Code}}</option>
</select>

我的component

public concept = [];
public datakartu: any = {cardno:""};
public empfile : any = {fullname:"",nik:"",disclimit:"",birthday:"",email:"",storeno:""};

constructor(
    private _router: Router,
    private _httpprovider: Httpprovider
) {}

ngOnInit() {

}

cardnof(newValue) {
    this.datakartu.cardno = newValue
    this._httpprovider.httpReq('http://192.168.1.40:5000/getdataemp','POST',{cardno:newValue},null).subscribe((data)=> { 
        var rData = [];
        for (let i=0;i<data.length;i++) {
            rData.push(data[i]);
        }

        var store = rData.map(i=>(i.Store_ID)).toString();

        this.empfile.storeno = store; << for changing the display on the combobox

console.log(this.empfile.storeno); << here i put the console log
    });
} 

我添加this.empfile.storeno = store;以便它将组合框的显示更改为我们从数据库获取的内容, 但它只是显示空白。

但当我console.log()时,值就在那里,只是字符串没有显示在屏幕上。enter image description here

基本上我所做的是填充卡号然后所有其他字段将填入来自数据库的数据但组合框不显示任何内容,但是当我执行控制台日志时它显示值

enter image description here

如果我做

的控制台日志,这就是结果
this.empfile.storeno = store;

1 个答案:

答案 0 :(得分:0)

要更改组合框上的显示,您必须将[(ngModel)]设置为对象,而不是仅将其设置为对象的某个属性。 不同的对象或文字,即使具有相同的值也不起作用。它必须是来自同一集合的对象。

&#13;
&#13;
var store = rData.map(i=>(i.Store_ID)).toString();

var filteredValues = concept.filter(i => i.MAP_Code === store);
 
if(filteredValues.length > 0) {
   this.empfile = filteredValues[0];
}
&#13;
&#13;
&#13;

并在html中设置ngModel到empfile



<select [(ngModel)]="empfile"  class="form-control">
    <option *ngFor="let p of concept" [ngValue]="p">{{p.MAP_Code}}</option>
</select>

这是假设,&#34;概念&#34;是任何类型的对象数组,可以设置为empfile。