1

I am learning Angular 2. I am trying to implement autosave functionality using (change) event. Like this

<div>
    <md-input class="ctrl" [(ngModel)]="first" (blur)="saveItemInfo()"></md-input>
</div>
<div>
    <md2-autocomplete [items]="items" item-text="name" item-value="value" [(ngModel)]="second" [disabled]="disabled"
        (change)="saveItemInfo()">
    </md2-autocomplete>
</div>
<div>
    <md-slide-toggle [(ngModel)]="third" color="primary" (change)="saveItemInfo()"></md-slide-toggle>
</div>

Here the problem is, change event is firing even when page loads. So that it is making so many api calls. Any other way to do this?

3
  • Do a check in saveItemInfo to see if anything actually changed. Commented Oct 17, 2016 at 6:04
  • Try to change (change) to (keyup) and check if it works for you. Commented Oct 17, 2016 at 6:08
  • @micronyks, (keyup) not working for md2-autocomplete. It is not firing event when change occurs. Commented Oct 17, 2016 at 6:14

1 Answer 1

1

You can keep a private variable like _second and on every (change) event check if anything changed:

  • If yes, set the private variable to the new value and call your API methods.
  • If not, do nothing
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.