5

I have following parent html

<p>Parent</p>
<app-child [mainData]="mainData"></app-child>

parent.ts

mainData = [];
ngOnInit() { 
 this.myService((res)=>{
 this.mainData = res;
})
}

Child html

<p>My JSON {{mainData | json}}</p> //Here getting result from parent

child.ts

@Input() mainData = [];
myDataCopy = [];
ngOnInit() { 
   this.myDataCopy = this.mainData; 
   console.log('My copy Data', this.myDataCopy); // Doesn't get result on here 
}

If I need to process the @Input data how it possible?

1
  • Could you share the implementation of myService() with us? the callback indicates that you are working with some asyncronous here, which would explain why your code does not work the way you intend Commented Oct 21, 2019 at 6:33

5 Answers 5

5

Try it in ngOnChanges lifecycle hook

child.ts

ngOnChanges(changes: SimpleChanges) {
  const mainDataChange = changes.mainData ;

  if (mainDataChange) {
    this.myDataCopy = this.mainData; 
    // or, this.myDataCopy = mainDataChange.currentValue;
    console.log('My copy Data', this.myDataCopy);
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

what you mean SimpleChanges?
SimpleChange class represents a basic change from a previous to new value.
3

You can try @Input getter and setter

  _mainData: any;
  @Input()
  set mainData( val ) {
    this._mainData = val;
  }

  get mainData: any {
    return this._mainData;
  }

And read more Here

Comments

1

Maybe syntax depend on Angular version...

Can you try to force "bindingPropertyName" argument in @Input declaration ?

like :

@Input('mainData') mainData = [];

Comments

0

Just to throw another alternative out there taken from this blog.

Stackblitz

export class ChildComponent implements OnChanges  {
  /* METHOD THREE 
   * Using a decorator
  */
  @OnChange<number>((value, simpleChange) => {
    console.log('OnChange decorator methodThree', value)
  })
  @Input()
  methodThree: number;

}

Decorator

export interface SimpleChange<T> {
  firstChange: boolean;
  previousValue: T;
  currentValue: T;
  isFirstChange: () => boolean;
}

export function OnChange<T = any>(callback: (value: T, simpleChange?: SimpleChange<T>) => void) {
  const cachedValueKey = Symbol();
  const isFirstChangeKey = Symbol();
  return (target: any, key: PropertyKey) => {
    Object.defineProperty(target, key, {
      set: function (value) {
        // change status of "isFirstChange"
        if (this[isFirstChangeKey] === undefined) {
          this[isFirstChangeKey] = true;
        } else {
          this[isFirstChangeKey] = false;
        }
        // No operation if new value is same as old value
        if (!this[isFirstChangeKey] && this[cachedValueKey] === value) {
          return;
        }
        const oldValue = this[cachedValueKey];
        this[cachedValueKey] = value;
        const simpleChange: SimpleChange<T> = {
          firstChange: this[isFirstChangeKey],
          previousValue: oldValue,
          currentValue: this[cachedValueKey],
          isFirstChange: () => this[isFirstChangeKey],
        };
        callback.call(this, this[cachedValueKey], simpleChange);
      },
      get: function () {
        return this[cachedValueKey];
      },
    });
  };
}

This method uses a typescript decorator. IMHO decorators are likely to stay but...

Decorator are an experimental feature that may change in future releases

Comments

0

app.components

import {   Component,  Inject,  Input,  OnInit, SimpleChanges} from '@angular/core';

export class ResultsComponent implements OnInit {
  @Input() public employeeData!: Array<Insight>;
  filterBy: any;

  constructor() {
  }


  ngOnChanges(changes: SimpleChanges): void {
    if (changes) {
       //To do filter
      // this.filterBy = this.employeeData.filter((obj) => {
      //   return obj.joiningDate === '2022-10-01T00:00:00.000Z';
      // });

      //Assign Input Data to Var
      this.filterBy = this.employeeData!;
      console.log(this.filterBy);
    }
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.