简体   繁体   English

ngModel表演奇怪 - Angular2

[英]ngModel Acting Strange - Angular2

myList: any;
ngOnInit(){
    this.myList = [
        {id: 1, name: 'a'},
        {id: 2, name: 'b'},
        {id: 3, name: 'c'},
        {id: 4, name: 'd'},
    ];
}

In my html 在我的HTML中

<input *ngFor="let l of myList" [(ngModel)]="l.name" name="{{l.id}}" />

It's working good as below: 它的工作原理如下:

在此输入图像描述

Chrome Dev: Chrome Dev:

在此输入图像描述

Then when I call another service in my ngOnInit like below: 然后,当我在ngOnInit中调用另一个服务时,如下所示:

myList: any;
ngOnInit(){
    this.myList = [
        {id: 1, name: 'a'},
        {id: 2, name: 'b'},
        {id: 3, name: 'c'},
        {id: 4, name: 'd'},
    ];

    this.outletService.getAll().then(outlets => {
         this.outlets = outlets;
    });
}

Suddenly, there is no value in the input as you see below: My html code is the same. 突然,输入中没有值,如下所示:我的html代码是相同的。

在此输入图像描述

在此输入图像描述

What could possibly be happened? 可能会发生什么? How can I get the value to appear in the inputs? 如何将值显示在输入中?

Here is the outletService .getAll() 这是outletService .getAll()

getAll() {
    return new Promise(resolve => {
        let headers = new Headers({
        'Content-Type': 'application/json'
        });
        let options = new RequestOptions({ headers: headers });
        this.http.get(this.host + '/outlets', options)
            .subscribe(data => {
                resolve(data.json()); //Return array of objects
            });
    });
}

Here is the return from the outletService: 这是来自outletService的回报:

在此输入图像描述

There is no error in the console, and I want the property name from this.myList , not outletName from this.outlets . 控制台中没有错误,我希望this.myList的属性name ,而不是outletNamethis.outlets

在此输入图像描述

Update 更新

What's more interesting is: I don't even need to call another service: If i just assign new array to this.outlets, it won't work, like this case: 更有趣的是:我甚至不需要调用另一个服务:如果我只是将新数组分配给this.outlets,它将无法工作,就像这样:

myList: any;
outlets: any;
ngOnInit(){
    this.myList = [
        {id: 1, name: 'a'},
        {id: 2, name: 'b'},
        {id: 3, name: 'c'},
        {id: 4, name: 'd'},
    ];
    this.outlets = [
        {id: 1, name: 'a'},
        {id: 2, name: 'b'},
        {id: 3, name: 'c'},
        {id: 4, name: 'd'},
    ];
}

And add another line to the html file. 并在html文件中添加另一行。

<input *ngFor="let l of myList" [(ngModel)]="l.name" name="{{l.id}}" />
<input *ngFor="let o of outlets" [(ngModel)]="o.outletName" name="{{o.id}}" />

You should change your template. 您应该更改模板。 There is no name attribute on your returned data: 返回的数据没有name属性:

<input *ngFor="let l of myList" [(ngModel)]="l.outletName" name="{{l.id}}" />

Ohh, well.. Gunter already mentioned this in his comment :) 哦,好吧...... Gunter在评论中已经提到了这个:)

In fact, the ngModel doesn't give any values to the input because the id properties of the two objects are the same! 实际上,ngModel没有给输入赋值,因为两个对象的id属性是相同的!

If I change from this: 如果我改变了这个:

myList: any;
outlets: any;
ngOnInit(){
    this.myList = [
        {id: 1, name: 'a'},
        {id: 2, name: 'b'},
        {id: 3, name: 'c'},
        {id: 4, name: 'd'},
    ];
    this.outlets = [
        {id: 1, name: 'a'},
        {id: 2, name: 'b'},
        {id: 3, name: 'c'},
        {id: 4, name: 'd'},
    ];
}

To this: 对此:

myList: any;
outlets: any;
ngOnInit(){
    this.myList = [
        {id: 1, name: 'a'},
        {id: 2, name: 'b'},
        {id: 3, name: 'c'},
        {id: 4, name: 'd'},
    ];
    this.outlets = [
        {id: 5, name: 'a'},
        {id: 6, name: 'b'},
        {id: 7, name: 'c'},
        {id: 8, name: 'd'},
    ];
}

Then it works! 然后它的作品! ask me why? 问我为什么? I don't know.. If someone can explain would be great! 我不知道..如果有人能解释会很棒!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM