Stefan
Stefan

Reputation: 2178

Changing ng-model for input inside ng-repeat based on content

I have an exercise where I have to be able to customise some options of a laptop. I'm loading this options from the database. There are four possible options: RAM,Processor,Video,Screen. For the input model, I would like to set the ng-model respectively.

<div class="text-center">
<div class="marginTop">

    <p>{{laptop.selected.text}}</p>

    <div class="row" ng-repeat="x in laptop.customize">
        <div class="col-xs-3">
            <p>{{x.name}}</p>
        </div>
        <div class="col-xs-9">
            <label class="radio-inline" ng-repeat="y in x.options">
                <input type="radio"  name="{{x.name}}" ng-model="laptop.selected.{{x.name}}" ng-value="y.description">            {{y.description}}
            </label>
        </div>
    </div>

    <div class="col-xs-12">
        <a href="#order" ng-click="laptop.save()" class="btn btn-default" type="submit">En door</a>
    </div>
</div>

The goal would be to set ng-model="laptop.selected.RAM" or ng-model="laptop.selected.Processor" , but I can't find any way to do so.

Excuse my html, I'm a noob.

[edit] JSON in laptop.customize

[
{
"name": "RAM",
"options": [
  {
    "id": 1,
    "description": "4gb",
    "customizationlaptopid": 1,
    "pricedifference": -50
  },
  {
    "id": 2,
    "description": "6gb",
    "customizationlaptopid": 1,
    "pricedifference": 0
  },
  {
    "id": 3,
    "description": "8gb",
    "customizationlaptopid": 1,
    "pricedifference": 100
  }
]
},
{
"name": "Processor",
"options": [
  {
    "id": 4,
    "description": "i3 2.3Ghz",
    "customizationlaptopid": 2,
    "pricedifference": -100
  },
  {
    "id": 5,
    "description": "i5 2.7Ghz",
    "customizationlaptopid": 2,
    "pricedifference": 0
  },
  {
    "id": 6,
    "description": "i7 3.2GHz",
    "customizationlaptopid": 2,
    "pricedifference": 150
  }
]
},
{
"name": "Video",
"options": [
  {
    "id": 7,
    "description": "NVidia 720M 1GB",
    "customizationlaptopid": 3,
    "pricedifference": -100
  },
  {
    "id": 8,
    "description": "Nvidia 740N 2GB",
    "customizationlaptopid": 3,
    "pricedifference": 0
  },
  {
    "id": 9,
    "description": "Nvidia 980NX 6GB",
    "customizationlaptopid": 3,
    "pricedifference": 200
  }
]
}
]

Upvotes: 0

Views: 30

Answers (1)

Sajeetharan
Sajeetharan

Reputation: 222532

You just set like this, since you already hold the values in y, also you can get the selected value using ng-change

<div class="marginTop">
    <p>{{laptop.selected.text}}</p>
    <div class="row" ng-repeat="x in laptop.customize">
        <div class="col-xs-3">
            <p>{{x.name}}</p>
        </div>
        <div class="col-xs-9">
            <label class="radio-inline" ng-repeat="y in x.options">
                <input type="radio"  name="{{x.name}}" ng-model="laptop.selected[y.name]" ng-change="getselected(y)"  ng-value="y.description">            {{y.description}}
            </label>
        </div>
    </div>
    <div class="col-xs-12">
        <a href="#order" ng-click="laptop.save()" class="btn btn-default" type="submit">En door</a>
    </div>
</div>

DEMO

Upvotes: 1

Related Questions