Angular 2 Binding in Component

Asked

Viewed 1,532 times

4

I have the following scenario.

I have a list in one webservice, but when I expect a return to pass the value to my component, it just doesn’t do the Binding because it takes time, and even when it does, in case I call the WS again, it does not update the data.

Below is code of how I am trying to implement. I did this example on top without putting the imports, just to put here and expose the way I’m trying to implement.

@Component({
    template:'<div *ngFor="let dados of lista">{{dados.nome}}<br/></div>',
    selector: 'meu-component'
})
MeuComponent {
    private _vaiPraView: any;
    @Input() testes: any;

    ngOnInit() {
        this.trataDados();
    }

    trataDados () {
        this._vaiPraView = [];
        let idx = 0;
        for(let teste of this.testes) {
            teste.realvector = idx;
            idx++;
            this._vaiPraView.push(teste);
        }
    }
}

//html... com binding (não rola...)
@Component({
    template:'<meu-component [testes]="lista"></meu-component>',
    directives: [MeuComponent]
})
export class TestePage {
    private lista: any;

    constructor() {

    }

    ngOnInit () {
        //Um evento demorado.. formato de data exemplo: [{nome: 'oi'},{nome: 'tudobem'},{nome: 'contigo'}]
        AlgumaCoisa.then(data => {
            this.lista = data;
        });
    }
}

//html... sem binding rola porém quero que fique do jeito acima e detecte a alteração.
@Component({
    template:'<meu-component [testes]="[{nome: 'oi'},{nome: 'tudobem'},{nome: 'contigo'}]"></meu-component>',
    directives: [MeuComponent]
})
export class TestePage {
    private lista: any;

    constructor() {

    }

    ngOnInit () {
        //Um evento demorado.. formato de data exemplo: [{nome: 'oi'},{nome: 'tudobem'},{nome: 'contigo'}]
        AlgumaCoisa.then(data => {
            this.lista = data;
        });
    }
}
  • In the first part there MeuComponent { ... you didn’t use the export class, could the problem be there?

  • I do not believe that was it @wdarking ... Wow how long this question rs... It was starting at angular yet.

4 answers

0

What happens is that the treatDados only runs once in the Oninit of the Component. So we need to run data processing right after getting the data to solve the problem.

In my-Component.ts

  1. Exchange the ngOnInit for ngOnChanges, so when the request response is finished it will change the array and update the Component.
     export class MeuComponentComponent implements OnChanges {
   
     ngOnChanges(){
       this.trataDados();
     }
  1. Remove the private from the property _vaiPraView for it to be accessible in the template (my-Component.html)
    _vaiPraView: any;

In test-page.component.ts

  1. Remove the private from the property list

Tips

  • Avoid error if data is not yet ready
trataDados () {
    if(!this.testes || this.testes.length == 0) {
      return;
    }

Reference:

0

You need to call the function trataDados() ngOnChange to update the field lista of the component MeuComponent when the input testes be updated.

0

The right one would be to use a variable of type Observable... and use the method subscribe... this ensures that it will be executed after the return...

metodo.subscribe(result => {
   this.lista = result.json();
   console.log(result);
});

0

Modify the first component to run the function every time you "receive" new data you can do so

@Component({
    template:'<div *ngFor="let dados of lista">{{dados.nome}}<br/></div>',
    selector: 'meu-component'
})
MeuComponent {
    private _vaiPraView: any;
    @Input() testes: any;

    
    @Input('testes')
    set name(testes: any[]) {        
        this._vaiPraView = [];
        let idx = 0;
        for(let teste of testes) {
            teste.realvector = idx;
            idx++;
            this._vaiPraView.push(teste);
        }
    }

    ngOnInit() {        
    }    
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.