对集合元素进行双重绑定不起作用

时间:2018-11-28 10:31:04

标签: typescript ngmodel two-way-binding

你好,我遇到以下问题。

我正在尝试使用ngFor指令来打印类似地图结构的每个value-pair,但是由于某些原因,看来ngModel指令不起作用。收集起来很好。如果我以一种方式打印绑定它,它仍然会打印成对的行。尝试双向绑定时,它只会显示第一个键值对。

export interface KVPair{
    key:string;
    value:string;
}
export class KVMap{
    items:Array<KVPair>;
    public getPair(key:string):KVPair{
        return {key:key,value:(this.items[key]).toString()};
    }
    constructor() {
       this.items=new Array<KVPair>();

    }
}

组件

@Component({
  selector: 'app-editor-view',
  templateUrl: './editor-view.component.html',
  styleUrls: ['./editor-view.component.css','../../Styles/common-controls.css']
})
export class NodeEditorComponent implements OnInit {
 @Input()
 public model:TreeModel;

 public map:KVMap=null;

  private formMap(model:TreeModel){
    if(model.payload==null){
      return null;
    }
    this.map=new KVMap();
    this.map.items.splice(0);
    var keys=Object.keys(model.payload);
    for(let k of keys){
      this.map.items.push({key:k,value:model.payload[k]});
    }
    this.map.items.forEach((kv)=>{
        console.log("key:"+kv.key+" - "+ "value:"+kv.value);
    })
  }
  constructor() { }
  onSubmit(form:NgForm){
  }
  ngOnInit() {
    this.formMap(this.model);
  }
}

HTML

<div>
    <form #jsonForm="ngForm">
        <div class="editor-header"> </div>
        <div class="editor-content">
            <ul class="list">
                <li *ngFor="let item of this.map.items">
                    key:{{item.key}}-value:{{item.value}}   //prints it fine
                    <div class="formPair">
                        <div class="key">
                            <div class="hblock">
                                <input class="hblock" [(ngModel)]="item.key" type="text" name="item.key">
                            </div>    //prints only the first kvpair
                        </div>
                        <div class="value">
                            <div class="hblock">
                                <input class="hblock" [(ngModel)]="item.value" type="text" name="item.value">
                            </div>
                        </div>
                    </div>
                </li>
            </ul>
        </div>
        <div class="editor-footer">
        </div>
    </form>
    {{jsonForm.value |json}}
</div>

模型和示例

  export interface TreeModel{
    id:string;
    payload?: any;
    children:Array<TreeModel> ;
}

let a: TreeModel = {
      id: "Adrian",
      payload: {
        profession:"doctor",
        age: 33,
        city: "Bucharest"
      },
      children: [
        {
          id: "Dan",
          payload: {
            married:true,
            children:99
          },
          children: [
            {
              id: "Daniel", children: [
                { id: "Ionut Mirea", children: [] }
              ]
            }
          ]
        },
        {
          id: "Marian", payload:{
            sex:"Masculine",
            wage:2000
          },children: [
            { id: "Farcas", children: [] },
            { id: "Liviu", children: [] }
          ]
        }
      ]
    };

0 个答案:

没有答案