4

For example, I have component:

@Component({
  selector: 'my-selector',
  inputs: ['data'],
  moduleId: module.id,
  template: '<p>{{transformedData}}</p>'
})
export class MyComponent {
  public transformedData: string;
  @Input() public data: string;

  // How to call this event on "data" change?
  public someEvent() {
    this.transformedData = this.data + '!';
  }
}

How to call someEvent() on data changes?

6
  • 2
    I wonder what is moduleId metadata? Commented Mar 27, 2016 at 14:52
  • 1
    @micronyks It is for relative Urls, more info: schwarty.com/2015/12/22/… Commented Mar 27, 2016 at 14:56
  • Thnaks for your share ! Commented Mar 27, 2016 at 14:57
  • i'm using commonJs, my HTML/ts files are in app folder but compiled files i.r js files in another dist folder from where my app ran, now when i use moduleId: module.id it took path from 'dist/blabla...' but there angular2 found only .js files not HTML now how to tell angular to take path from src folder instead of dist folder ? Commented Mar 28, 2016 at 7:29
  • @Pardeep Jain, You can copy HTML files into dist/blabla too Commented Mar 28, 2016 at 11:29

1 Answer 1

3

onchanges will get fired when data is changed.So, within onChanges you can call someEvent function.

export class MyComponent {
  public transformedData: string;
  @Input() public data: string;

  ___________________________________________________________________________________________
  // EDIT: you can also do it. Not necessary but can be done this way too.
 //  Note: If you go with this approach, you don't need to use onchanges hook and don't require above @Input public data line.
    @Input() set data(data:string)
           console.log(data);
           this.someEvent();
   }
 ____________________________________________________________________________________________ 


  ngOnChanges(...args: any[]) {

        console.log(args);
        this.someEvent();
  }

  // How to call this event on "data" change?
  public someEvent() {
    this.transformedData = this.data + '!';
  }
}
Sign up to request clarification or add additional context in comments.

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.