npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

form-control-change-tracker

v0.0.2

Published

Very often when developers need to know if there were any changes inside the a form in order to present a unsaved changes confirmation dialog when navigating away or in order to disable the save button when there is nothing new to save. The `FormControlCh

Downloads

285

Readme

Angular Form Control Change Tracker

Very often when developers need to know if there were any changes inside the a form in order to present a unsaved changes confirmation dialog when navigating away or in order to disable the save button when there is nothing new to save. The FormControlChangeTrackerModule provides two things:

  • The ChangeTrackerDirective (hgChangeTracker) that can be set on the individual form controls in order to track if any changes are made

  • And the @hasChanges() decorator that is applied over the ChangeTrackerDirective directives in order to provide you a boolean value indicating if there are any changes or not.

Usage:

  1. Import the module

your.module.ts

@NgModule({
  ...
  imports: [
    ...
    FormControlChangeTrackerModule
  ]
})
export class AppModule { }
  1. Add the directives and bind the initial value for each one.(the current example is using reactive forms but the module can be used with template driven forms as well. Check out the demo app)

your.component.html

<form [formGroup]="form" (ngSubmit)="submit()">
  <div class="form-group">
    <label>First Name</label>
    <input type="text" name="firstName" formControlName="firstName" hgChangeTracker
      [initialValue]="firstNameDefaultValue" />
  </div>
  <div class="form-group">
    <label>Last Name</label>
    <input type="text" name="lastName" formControlName="lastName" hgChangeTracker
      [initialValue]="lastNameDefaultValue" />
  </div>
  <button [disabled]="!hasFormChanges">Submit</button>
</form>
  1. Get the hasFormChanges value

your.component.ts

@Component({
  ...
})
export class TemplateFromComponent {

  @ViewChildren(ChangeTrackerDirective) @hasChanges() hasFormChanges: boolean;

  ...

}

Check out the demo app

  1. More configurations

Decorator configuration

your.component.ts

@Component({
  ...
})
export class TemplateFromComponent {

  // ChangesWithValues<T> { hasChanges: boolean; values: { [P in keyof T]: { current: T[P]; initial: T[P]; }; }; } (very useful for debugging)
  @ViewChildren(ChangeTrackerDirective) @hasChanges({ includeChangedValues: true }) formChangesData: ChangesWithValues<T>;
  ...
}

Inputs

change-tracker.directive

@Directive({
  selector: '[ngModel][hgChangeTracker],[formControl][hgChangeTracker],[formControlName][hgChangeTracker]',
  exportAs: 'hgChangeTracker'
})
export class ChangeTrackerDirective {

  @Input() multiInitialValue = false; // used for multiple initial values
  @Input() autoInitialValueSync = true; // it can be used to disable the auto syncing of initialValue input

  // whenever the auto sync is disabled this method needs to be manually called in order for the new initial value to be set
  // keep in mind that the newValue is optional and if omitted the last change of the initialValue binding will be the new initial value
  resetInitialValue(newValue?: T) { ... }
}