How can I concatenate the value of an ng model with the value from ng-repeat

I want to iterate over an array of objects that I get from a REST service and create a dynamic form using a directive ng-repeat.

This is my form with a rating directive (taken for the UI Bootstrap library)

<form name="categoryRatingFrom" data-ng-submit="updateCategories(catRatings) >

<div data-ng-repeat="cats in categories" class="form-group clearfix">
  <label class="control-label">{{ cats.name }}</label>
    <div class="no-outline"
          data-rating
          data-ng-model=" // Here I want to concatenate {{ cats.id }} with the ng-model name catRatings // "
          data-max="6"
          data-rating-states="ratingOptions.ratingStates"
          data-on-hover="atmosphereRating.onHover(value)"
          data-on-leave="atmosphereRating.onLeave()"></div>
     </div>
<form>

I want to set the value data-ng-modelusing the name of the object that I transmit when sending, and the identifier of the current object tin my loop / array, however I seem to be unable to do this. Do I have to concatenate in the controller when getting an array of objects using a loop, and then set the ng data model using the value from ng-repeat. Nothing is passed to the controller when the form is submitted (see My code below):

// loop through the object adding a ng-model name that we match in our form...
for (var i = 0, l = $scope.categories.length; i < l; i++) {
  $scope.categories[i]['modelId'] = 'catRatings.' + $scope.categories[i].id;
}

HTML data-ng-model="cats.modelId", - - , ?

0
2

ng-model , . , , , - :

ng-model="catRatings[cats.id]"

, , ng-model.

0

, . .

$scope.data = [
  {
    'id' : 0,
    'name' : 'Tim'
  },
  {
    'id' : 1,
    'name' : 'John'
  }
];
$scope.ratings = [ '5 stars', '2 stars' ];

'catRatings' + {{ cats.id }}, , - $scope.catRatings1, $scope.catRatings2 .. .

<label ng-repeat="person in data">
  <input type="text" ng-model="person.name">
  ...
</label>

<label ng-repeat="person in data">
  ...
  <input type="text" ng-model="ratings[$index]">
</label> 

id...

<label ng-repeat="person in data">
  ...
  <input type="text" ng-model="ratings[person.id]">
</label>
-1

Source: https://habr.com/ru/post/1655854/


All Articles