Asagohan
Asagohan

Reputation: 593

ng-attr not changing to true

I have an input that looks like this:

<input  type="text" autocomplete="off" name="itemCode" ng-model="item.itemCode" class="form-control" ng-attr-existing-item-validator="mode=='Add'" required>

which always called existing-item-validator directive even if mode is not equal to 'Add', so I tried

<input  type="text" autocomplete="off" name="itemCode" ng-model="item.itemCode" class="form-control" ng-attr-existing-item-validator="isAddMode()" required>

However, isAddMode is never executed on my scope. If I change the input to ng-attr-existing-item-validator="{{isAddMode()}}" it does execute, but existing-item-validator directive is still getting called even though isAddMode() is returning false.

Am I doing something wrong with the ng-attr attribute? I expect my directive not to be called if it is equal to false.

Upvotes: 0

Views: 227

Answers (1)

Icycool
Icycool

Reputation: 7179

Setting directive parameter to false doesn't mean the directive is not created, it means you are passing a variable into the directive which can be retrieved by scope or attr.

While I'm not sure if you can activate a directive dynamically, you can handle the behavior in the directive (e.g. turning off itself) when according to the parameter.

.directive('existingItemValidator', function(){
  return function (scope, element, attr) {
    if (attr.existingItemValidator === false) return;

    // continue other ops.
  }
});

Note: if you are using attr, you need to use {{}} to pass your parameter

Upvotes: 0

Related Questions