Nik Tsekouras
Nik Tsekouras

Reputation: 289

Echo required attribute from json (dynamic creation of forms) angular

I am building my first angularjs app and my project has 3 entities. I am getting their model in json form. for example

return [
            { "name": "IsActive", "type": "checkbox", "default": "true", "icon": "check" },
            { "name": "Name", "type": "text", "required": "true", "default": "", "icon": "user" },
            { "name": "Email", "type": "email", "validation": "email", "default": "", "icon": "envelope" },
            { "name": "Phone", "type": "text", "default": "", "icon": "phone" },
            { "name": "Fax", "type": "text", "default": "", "icon": "fax" },
            { "name": "Mobile", "type": "text", "default": "", "icon": "mobile" },
            { "name": "Address", "type": "text", "default": "", "icon": "map-marker" },
            { "name": "ContactPerson", "type": "text", "default": "", "icon": "comment" }
        ];

With the entities' model i dynamically create their add forms. Here is part of my html:

<div class="addInpRow row" ng-repeat="m in vm.accounts.emodel">
   <input name="{{m.name}}" type="{{m.type}}"/>
 </div>

What i want to do is echo the required attribute if it is true.

I have tried several things to print it but nothing does the job. Some things that i have tried are:

<input name="{{m.name}}" {{m.required ? 'required' : ''}} />
<input name="{{m.name}}" aria-required="{{m.required}}" />

and many variations of what my m.required looks, like 'required="required".

Any help would be much appreciated. Thanks

Upvotes: 0

Views: 54

Answers (2)

Shashank Agrawal
Shashank Agrawal

Reputation: 25797

Use

<input name="{{m.name}}" ng-required="m.required">

Upvotes: 1

Explosion Pills
Explosion Pills

Reputation: 191749

Use ng-required instead.

<input name={{m.name}} ng-required=m.required>

Upvotes: 1

Related Questions