javascript - 根据内容更改 ng-repeat 内输入的 ng-model

标签 javascript angularjs

我有一个练习,我必须能够自定义笔记本电脑的某些选项。我正在从数据库加载此选项。有四种可能的选项:RAM、处理器、视频、屏幕。对于输入模型,我想分别设置 ng-model 。

<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>

目标是设置 ng-model="laptop.selected.RAM"ng-model="laptop.selected.Processor" ,但我可以找不到任何方法来做到这一点。

请原谅我的 html,我是个菜鸟。

[编辑] laptop.customize 中的 JSON

[
{
"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
  }
]
}
]

最佳答案

你只需这样设置,因为你已经保存了 y 中的值,你也可以使用 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

关于javascript - 根据内容更改 ng-repeat 内输入的 ng-model,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40831809/

相关文章:

javascript - Mootools 中 for (var x in data) 循环发生了什么

javascript - 使用 javascript 添加 onclick 事件监听器和元素

javascript - ng-重复 : How to set property multiple in select

javascript - 使用 Angular.js 的解析而不是 Backbone

javascript - 用于多选的 AngularJS 指令

javascript - Ng-Repeat 改变变量

javascript - 将 JSON 插入 MongoDB 自动从字符串转换日期

javascript - 为什么 Javascript 'every' 函数返回 false?

javascript - 在 JavaScript 中选择选项 html

javascript - 使用 Angular Directive(指令)中的参数在 css 中动态转换