Jan Jan - 3 months ago 9
AngularJS Question

Use ng-repeat variable inside ng-model names

<ng-model="formData.alert_settings.{{alert}}" ng-repeat="alert in alerts">


I wanted ng-model names to be
formData.alert_settings.down
,
formData.alert_settings.up
, etc.

where :

alerts={down,up,trouble , ...}


This way
formData.alert_settings.{{alert}}
is not working.
Please help me.

Answer

If you have this in your controller:

$scope.formData = {alert_settings: {}}
$scope.alerts=['down','up','trouble'];

Then you can do this

<input type="text" ng-model="formData.alert_settings[alert]" ng-repeat="alert in alerts">

and you will end up with three inputs and the data will be in formData.alert_settings.down, and so on

Comments