AngularJS ng-required does not work with variables

advertisements

I have to use the directive ng-required, but I cannot hardcode true or false. I need to use it in a variable, but angular does not recognize the "true".

jsFiddle example

<div ng-app ng-init="test=true">
    <div>
        both have ng-required="true"
        <br/>
        one as hardcoded string literal, one via a variable
        <br/>
        Inspect them, one is not required!
    </div>
    <form name="myForm">
        <input type="text" ng-required="{{test}}" />
        <input type="text" ng-required="true" />
    </form>
</div>

How can I get this working?


<input type="text" ng-required="{{test}}" />

You don't need to interpolate test here. Since you want to evaluate the value of test, just remove the curly braces and it should work.