0
<div class="row" data-ng-repeat="i in [1,2,3,4]">
            <div asterick class="form-group" ng-class="{'form-group has-success': !error['resource_type'] && (submitted), 'form-group has-error': (error['resource_type']) && (submitted)}"> 
                <label for="resourceType">Resource Type</label>
                 <select name="resourceType" id="resourceType" ng-model="data.resource_type+{{ i }}" ng-required="true"  >
                    <option ng-repeat="option in resourceTypeJson" value="{{option}}">{{option}}</option>
                </select>
           </div>
</div>

I am trying to access that i (loop variable) with ng-model. I need resource_type1, resource_type2 and so on. How can I do this please help.

2 Answers 2

2

Make resource type an array instead of separate properties.

{
    data: {
        resource_type1: 'data1',
        resource_type2: 'data2',
        resource_type3: 'data3'
    }
}

becomes

{
    data: {
        resource_type: ['data1', 'data2', 'data3']
    }
}

and then you can do that following in your ngModel:

ng-model="data.resource_type[i]"

What if you can't do the above?

You can try the following:

ng-model="data['resource_type' + i]"
Sign up to request clarification or add additional context in comments.

4 Comments

can we use this in id and name as well like id="resourceType[+i]"
sorry I am very newbie in angular
@Ramkishan you need to use id="{{...}}" for non-angular attrs
Thank you @tanmay
0

change the ng model like htis

ng-model="data['resource_type' + i]"

This way ng model property name gonna change dynamically

Demo

angular.module("app",[])
.controller("ctrl",function($scope){
  $scope.data = {}

})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
 <div class="row" data-ng-repeat="i in [1,2,3,4]">
             
                 <select  name="resourceType" id="resourceType" ng-model="data['resource_type' + i]" ng-required="true"  >
                    <option ng-repeat="option in [1,2,3,4]" value="{{option}}">{{option}}</option>
                </select> 
</div>
{{data}}
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.