the-a-train
the-a-train

Reputation: 1143

Difference between using ng-model and custom attribute for 2 way binding on custom directive

When building a custom form input directive should i use ng-model or my own custom my-val attribute.

The outcome I want is 2 way binding so that changing the input updates the model on the $scope and changing the $scope updates the form input.

I understand that if I "require": "ngModel" in my directive then I get passed the ngModelController whereas using myVal will be a simple property.

Upvotes: 1

Views: 159

Answers (1)

New Dev
New Dev

Reputation: 49620

The ng-model serves two main purposes:

  1. DOM-agnostic abstraction layer that provides hooks for other directives (custom and built-in) to validate, format or parse the value
  2. integration with form directive to set $dirty/$pristine and $valid/$invalid flags

So, you could definitely have a custom two-way bound attribute, but then you lose out on using directives like ng-required or ng-change, and other custom directives that support ng-model model.

Upvotes: 1

Related Questions