sav vv
sav vv

Reputation: 77

ng-repeat with input text box track by $index

I am writing an application where a div hoolds datepicker, 2 timepicker and a input text box. and i have a checkbox outside of div. Each time when i check a checkbox one div of date location will add. My application works fine except my input box value. ng-model is not holding the text-box value

<div class="dateContainer"  ng-repeat="selecting in selects track by $index">
  <div class="datePicker">  
    <button bs-datepicker>
      --- some code
    </button>
  </div>
  <div class="timePicker">  
    <button bs-timePicker>
      --- some code
    </button>
  </div>
  <label class="configLocation" for="configLoc">Location</label>
  <input class = "form-control"type="text" ng-change ="DateArr($index)"  style="font-weight: normal;"name ="configLocation" class="configLocation"  ng-model = "selects[$index].configLoc" />
</div>

I kept my model name selects[$index].configLoc since i am trying to save based on each index value.

My controller- I am initializing $scope.selects like below.

$scope.selects = [{commDate : null,commEtime: null,commStime: null,configLoc: ''}];

When i call DateArr($index) on change event am unable to get the entered value $scope.selects[0].configLoc ?? value always showing ''. why??

Upvotes: 0

Views: 984

Answers (2)

Hemakrishnan
Hemakrishnan

Reputation: 66

you have to use $index in ng-repeat or ng-model ... just change :

ng-repeat="selecting in selects"
  (or)
ng-model="selecting.configLoc"

Upvotes: 1

yclee0210
yclee0210

Reputation: 131

For each ng-repeat a new scope is created with the repeated item, i.e. selecting.

ng-model = "selects[$index].configLoc"

becomes

ng-model = "selecting.configLoc"

Upvotes: 0

Related Questions