0

I'm using angular 1.5 and I have controller, but it not a component, just usual controller. I tried to use $onChanges in this, but it not working. So, is it actually possible to use $onChanges on a non component controller?

Amir Suhail
  • 1,284
  • 3
  • 12
  • 31
  • 1
    I think $onChanges is an event only available on component's controller. If you are not using component then you can just access controller's property maybe using $parent or other ways depending on your architecture. – KKS Feb 13 '17 at 09:38

1 Answers1

1

Components have a well-defined lifecycle Each component can implement "lifecycle hooks". One of the hooks is $onChanges

Reference

If you would like to detect changes and not using Angular 1.5 component feature then you have following options to go ahead:

  • Create a component so that you can make this part of your app reusable and make most of the lifecycle hooks it provides out of the box with this version of Angular.

Because you have not stated your actual use case, you may try following based on your needs:

  • If you are binding to an HTML5 input/select/radio elements then use ng-model for two-way data binding so that when user updates it your model will be updated automatically.
  • Combine ng-change with ng-model above so that you can add some extra behaviour whenever the value is changed. Reference
  • Use $watch if you really have no option to choose above and destroy it when its no longer needed. Reference

Also, please mention in your question what are you trying to achieve. A jsfiddle will be good and can get you more clear answers.

KKS
  • 3,600
  • 1
  • 27
  • 54
  • Unfortunately I can't create a Fiddle, Customer confidential code and stuff. But thanks for detailed explaination. – Amir Suhail Feb 13 '17 at 10:21