Error trying to diff '[object Object]' in Angular

前端 未结 4 893
情书的邮戳
情书的邮戳 2020-11-27 20:28

Looks like something wrong with freight variable in HTML:

Error in app/freightList.component.html:13:8 Error trying to diff \'[object Object]\'

相关标签:
4条回答
  • 2020-11-27 21:07

    I ran into this issue when I changed the WebApi I was calling to return a Task<IActionResult> instead of the previous IEnumerable<object>. Check the response isn't wrapped in an object. I had to change my extractData method to:

    private extractData(res: Response) {
       let body = res.json();
       return body.result || body || { };
    }
    
    0 讨论(0)
  • 2020-11-27 21:10

    Your extractData (and possibly also your HTTP API) is returning an object {} - ngFor requires an array [] to iterate.

    Change your service/API to produce an array, or transform the object in your component.

    0 讨论(0)
  • 2020-11-27 21:15

    The best way is to take the NgForm object and call its reset() function.

    Example:

    Html file

    <form #exampleform='ngForm'>
    
    </form>
    

    ts file

    @ViewChild('exampleform') exampleform :NgForm;
    
    this.exampleform.reset(); // resets the form
    
    0 讨论(0)
  • 2020-11-27 21:16

    The problem (for me) was in my newState definition. Below is the correct definition:

    const newState = (state, newData) => {
        return Object.assign([], state, newData);
    }
    

    My newState was converting my array to an object because I was using the wrong brackets - the Wrong Way is below.

    const newState = (state, newData) => {
        return Object.assign({}, state, newData);
    }
    

    Good luck, I hope that helps, Rob

    0 讨论(0)
提交回复
热议问题