1

I want to only set my attribute of ng-minlength when the value I parse from data.Validation['minlength'] is not nil.

First I attempted using ng-switch; I had no problem doing this when handling ng-show=true/false. However I was unable to get it working when it was beyond just the value but also the whole declaration of ng-minlength="...". Second attempt was using ng-if but again I was unable to get it working in the "middle" of the input.

Below is some code that works - I want the whole ng-minlength="data.Validation['minlength']" to only be set if the value is not nil.

<input type="text" name="foo" ng-model="item.foo" 
ng-minlength="data.Validation['minlength']" required/>
Nicklas Kevin Frank
  • 6,079
  • 4
  • 38
  • 63

2 Answers2

1

I discovered a simpler approach in another question of mine.

Solution by Karaxuna here.

<input type="text" name="foo" ng-minlength="myvar || 0" required/>
Community
  • 1
  • 1
Nicklas Kevin Frank
  • 6,079
  • 4
  • 38
  • 63
0

You can try ng-switch-on

 <span ng-switch on="data.Validation>
    <input ng-switch-when="null" type="text" name="foo" ng-model="item.foo" ng-minlength="data.Validation['minlength']" required/>
    <input ng-switch-default type="text" name="foo" ng-model="item.foo" required/>
</span>

It will create input depending on data.Validation values

However I am not sure if it will work with NULL values. It is still a good way forward.

Michał Lach
  • 1,291
  • 4
  • 18
  • 37