Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 SimpleChanges Object throws error at first npm start

In my angular 2 application there is a component containing an array of objects that is passing the chosen (clicked) one to it's direct child component. This does display the data more detailed. I'm using the "SimpleChanges" feature to watch in this child component if the object given changed to make another http request to get the related comments from a database.

If I try to build it with npm I get an error, saying :

app/displayEntry.component.ts(23,41): error TS2339: Property 'entry' does not exist on type 'SimpleChanges'

If I just comment this part out, start npm and finally put it in there again and save it, there is no Problem anymore ( no erro and it works ). My question is, is there a way to work around this behavior and can this cause any trouble later I don't foresee or should I just ignore it? Thanks for your help

Parent component:

import { Component, OnInit } from '@angular/core';
import { entry } from './Objekte/entry';
import { entryService } from './entry.service'

@Component({
templateUrl: 'app/Html_Templates/displayLastEntrys.template.html'
})

export class displayLastEntrys implements OnInit{

public entrys : entry[];
private entryChoosen: boolean;
private ChoosenEntry : entry; 

constructor ( private entryservice : entryService){
    this.entryChoosen = false;
}

ngOnInit() : void  {
    this.getData();
}

getData() {
    this.entryservice.getFirstEntrys().then(entrys => this.entrys = entrys);
}

entryClicked(ent: entry){
    this.entryChoosen = true;
    this.ChoosenEntry = ent; 
}

leaveEntry () {
    this.entryChoosen = false; 
}

voted( upordown : boolean ) {

}
}

Child component:

import { Component, Input, Injectable, OnChanges , SimpleChanges, Output,                    EventEmitter} from '@angular/core';
import { entry} from './Objekte/entry';
import { entryService } from './entry.service';
import { comment } from './Objekte/comments'; 

@Component({
selector: 'display-entry',
templateUrl: 'app/Html_Templates/displayEntry.template.html'
})

export class displayComponent implements OnChanges{
@Input() public entry : entry;
public comments : comment[];

private changecounter : number;

constructor(private service : entryService) {
    this.changecounter  = 0;
}

ngOnChanges(changes : SimpleChanges){

   this.service.getComments(changes.entry.currentValue.id)
            .then(com => this.comments = com )
            .catch();

    this.entry.viewed++;
    // To implement :: change database 
}

votedUp () : void {
    this.entry.votes ++;
    // To implement :: change database 
}

votedDown () : void  {
    this.entry.votes --;
    // To implement :: change database
}
}
like image 225
Lucas_R Avatar asked Sep 29 '16 07:09

Lucas_R


People also ask

Does ngOnChanges run on init?

With this update, ngOnChanges immediately fires. It also fires upon initialization of input data. The hook receives one optional parameter of type SimpleChanges . This value contains information on the changed input-bound properties.

What is SimpleChanges in angular?

SimpleChanges is an Angular/Core feature that can be used to see the changes and a few more details of the declared property names in a component. And also it needs to be used in the Angular ngOnChange method to see the values changes and to do relevant things.

What is ngOnChanges?

ngOnChanges gets called before ngOnInit and whenever a component's bound input is changed FROM THE PARENT COMPONENT. Remember that ngOnChanges is specific to bound inputs on the component. This means if you don't have any @Input properties on a child, ngOnChanges will never get called.

Which option can be used to capture the previous value current value first change is first change properties in angular?

SimpleChangelink Represents a basic change from a previous to a new value for a single property on a directive instance.


2 Answers

The accepted solution is suboptimal for TypeScript, as you're defeating the type system.

SimpleChanges does not have an entry property, so the compiler quite rightly balks. The solution is to treat the changes object as an array:

ngOnChanges(changes : SimpleChanges){
  if (changes['entry']) {
    this.service.getComments(changes['entry'].currentValue.id)
  }
}

Then you can continue to strongly type the ngOnChanges method.

like image 139
superluminary Avatar answered Oct 19 '22 23:10

superluminary


To make the compiler not complain just change your method definition for parameter one from SimpleChanges to any:

ngOnChanges(changes: any) {

//...
like image 25
rinukkusu Avatar answered Oct 19 '22 23:10

rinukkusu