• About Morris Development
  • A Focus for Cloud Efficiency
    • Microsoft Azure
    • Amazon Web Services
  • What our Clients Say
  • Our Products
    • PreschoolDB
    • WebinarDB
  • Contact Us

Morris Development

Custom System Development and Integration

August 25, 2016

Replacing $watch capabilities with $onChanges in Angular Components using Typescript

The Angular feature $watch has become known for its tendancy to hog memory, so we’ve been avoiding it wherever we can. However, with Components, the fact that you may only have a few bound values makes it much more necessary to have a watch on a value. There is a very easy way to do this.

Angular Components contain several hooks that are very handy. The $onChanges is especially powerful. In this example, I have a dropdown menu that I’d like to populate from a database API. The dropdown is always dependent on the office_id, so one of our bindings is officeId. However, if the user on the parent form changes the value of officeId from their own dropdown of offices, I need to reload the array of clients.

The key was trying to figure out how to capture the change. Here, I’ve stripped out the HTML and just given an example of the usage of $onChanges in a typescript file.

module Application.Components.Client {
  export class clientSelector {       
        public officeId: number;     
    
        static $inject = [ "jobFact"]      

        constructor( private jobFact) {  }

       $onChanges(changesObj) {                       
                if (changesObj.officeId) {
                    let msg = "Changed from " + changesObj.officeId.previousValue + " to " + changesObj.officeId.currentValue;                   
                        console.log(msg)                         
                     }           
        }
    }
 app.component("clientSelector", {
        template: ``
        , bindings: { officeId: "<" }
        , controller: clientSelector
        , controllerAs: "vm"
    });
}

So, what is happening here is the onChanges function is actually tracking all the bindings. When the value changes, the changesObj will have a .previousValue and a .currentValue. At that point, you can do whatever you want; change something in the component, like we were doing with dropdown when officeId changed, or even throw something back to the parent with a bound function.

All you need to know is that $watch is dead.

[disqus]

Article by MacGyver / AngularJS, TypeScript, Web Developer

About MacGyver

I've worked with database systems for over 20 years, and started my own company in 2000. Almost all my business consists of internal database systems, either ERP or CRM. My programming is primarily in Angular / Microsoft C# and MS SQL.

About This Site

Morris Development has been specializing in internal database system design and integration since 1999. We provide long-term management and support of secure data systems for many businesses as well as developing the more complex code structures for ERP systems like Intellievent, Apidas, and AVMS.

This site is primarily for our developers to keep track up various technologies and updates that are used by Morris Development.

Training

Integrating Angular Microsite with .Net

Private Data Caching with Google Storage

Continuous Deployment for Production Releases?

Azure Websites – the perfect Angular host

Angular 2

  • Angular 2 Authentication
  • Angular Command Line Interface
  • Material Design for Angular
  • Using Observables in Angular 2

Mentors

  • Ben Nadel
  • Dan Wahlin
  • Deborah Kurata
  • John Papa

Staff

  • Dan Morris

Training

  • Google Development Courses
  • Microsoft Virtual Academy
  • PluralSight
  • Test Deep Links

© 2025 · Morris Development