Ariel
Ariel

Reputation: 26753

In angular require a field only if it's not disabled

How can I require a field only if it's not disabled? This didn't work:

<form name="foo">
  <input type="text" name="bar" ng-required="!foo.bar.disabled">
</form>

Note that I am disabling the field directly through pure javascript, not with ng-disabled. I have a directive that runs through the form enabling/disabling things.

The directive listens to $on messages, and depending on what is gets will run:

element.find(':input').prop('disabled', true);

The $on messages come from a different scope, so I can't directly reference it and do ng-disabled=.....

Upvotes: 2

Views: 1501

Answers (1)

Charlie
Charlie

Reputation: 23778

Use ng-disabled with a scope variable. Your directive can then enable/disable the input through this scope variable. If your directive inherits from another scope, please use an intermediate factory to manage the scope variable.

$scope.barDisabled = dataSharer.barDisabled;

In above example the dataSharer is a factory and barDisabled directs to singleton.

In your form:

<form name="foo">
  <input type="text" name="bar" ng-required="!bartDisabled" ng-disabled="barDisabled">
</form>

Upvotes: 2

Related Questions