简体   繁体   English

在嵌套输入类型中使用ngOptions和/或ngRepeat选择

[英]Use ngOptions and/or ngRepeat in nested input type Select

I am trying to create an input type select , based off of the data grabbed from the db/json file . 我试图根据从db/json file的数据创建一个输入类型select The issue I am having is that I am able to the Make part of the select, but all the other sections are not getting updated when I am selecting an item. 我遇到的问题是,我能够在Make选择的一部分,但是当我选择一个项目的所有其他部分都不会得到更新。

Please Help Me. 请帮我。

Below is a screenshot of what I am trying to achieve: 以下是我要实现的屏幕截图:

在此处输入图片说明

HTML Markup: HTML标记:

<select required  ng-change="onCarChange(b,car)" ng-model="reportData.car.make" ng-options="bb.make for bb in cars" class="form-control" >
     <option value="">--Select--</option>
</select> 

<select required ng-model="reportData.car.model" ng-change="onModelChange(b,model)" ng-options="cha.model.name for cha in b.selectedModels" class="form-control" >
     <option value="">--Select--</option>
</select>

<select required ng-model="reportData.car.year" ng-change="onYearChange(b,year)" ng-options="t for t in b.selectedYears" class="form-control" >
    <option value="">--Select--</option>
</select>

<select required ng-model="reportData.car.color" ng-change="onColorChange(b,color)" ng-options="t for t in b.selectedColors" class="form-control" >
    <option value="">--Select--</option>
</select>

APP.JS Controller APP.JS控制器

  $scope.onCarChange=function(b,car){
    b.selectedModels=car.model;
  }
  $scope.onModelChange=function(b,model){
    b.selectedModel=model;
    b.selectedYears=model.years;

  }
  $scope.onYearChange=function(b,year){
    b.selectedYear=year;
    b.selectedColor=years.colors;

  }
$scope.onColorChange=function(b,color){
    b.selectedColor=color;

  }

Sample Data 样本数据

    $scope.cars = [
  {
    "model": [
      {
        "year": [
          "2016",
          "2015",
          "2014"
        ],
        "name": "CTS",
        "color": [
          {"id":1, "hexvalue":"#000", "description":"Black"},
          {"id":2, "hexvalue":"#fff", "description":"White"},
          {"id":3, "hexvalue":"#FAF0BE", "description":"Blonde"},
          {"id":4, "hexvalue":"#0000FF", "description":"Blue"},
          {"id":5, "hexvalue":"#808080", "description":"Grey"}
        ],
        "id": 0
      },
      {
        "year": [
          "2016",
          "2015",
          "2014",
          "2013",
          "2012"
        ],
        "name": "ATS",
        "color": [
          {"id":1, "hexvalue":"#000", "description":"Black"},
          {"id":2, "hexvalue":"#fff", "description":"White"},
          {"id":3, "hexvalue":"#FAF0BE", "description":"Blonde"},
          {"id":4, "hexvalue":"#0000FF", "description":"Blue"},
          {"id":5, "hexvalue":"#808080", "description":"Grey"}
        ],
        "id": 1
      },
      {
        "year": [
          "2016",
          "2015",
          "2014"
        ],
        "name": "XTS",
        "color": [
          {"id":1, "hexvalue":"#000", "description":"Black"},
          {"id":2, "hexvalue":"#fff", "description":"White"},
          {"id":3, "hexvalue":"#FAF0BE", "description":"Blonde"},
          {"id":4, "hexvalue":"#0000FF", "description":"Blue"}
        ],
        "id": 2
      }
    ],
    "make": "CADILLAC",
    "picture": "http://placehold.it/32x32",
    "index": 0,
    "_id": "573bef46573891a64081d4d6"
  }
}
]

The b you are referring to should be inside the $scope so it can be updated correctly on the html. 您所指的b应该在$ scope内,以便可以在html上正确更新。 Use $scope.b insted of b, just like you did with $scope.cars. 就像使用$ scope.cars一样,使用由b插入的$ scope.b。

If you want a more clean code, remove the b and use $scope.selectedModel instead of $scope.b.selectedModel, and inside ng-options selectedModel instead of b.selectedModel. 如果您想要更干净的代码,请删除b并使用$ scope.selectedModel代替$ scope.b.selectedModel,并在ng-options selectedModel内代替b.selectedModel。

Please modify as below : 请修改如下:

     $scope.onCarChange=function(b,car){
        $scope.b.selectedModels=car.model;
      }
      $scope.onModelChange=function(b,model){
        $scope.b.selectedModel=model;
        $scope.b.selectedYears=model.years;

      }
      $scope.onYearChange=function(b,year){
        $scope.b.selectedYear=year;
        $scope.b.selectedColor=years.colors;

      }
    $scope.onColorChange=function(b,color){
        $scope.b.selectedColor=color;

      }

Also one suggestion, you dont need to call ng-change to reflect the same. 还有一个建议,您无需调用ng-change即可反映相同的情况。 Consider below example 考虑下面的例子

Html code : first: {{ firstSelection }} second: {{ secondSelection }} HTML代码:第一:{{firstSelection}}第二:{{secondSelection}}

Js code: js代码:

var myapp = angular.module('myapp', []);
myapp.controller('myCtrl', function ($scope) {
    $scope.a = [1,2,3];
    $scope.b = {
        1:[1,2],
        2:[2,4],
        3:[3,6]
    };
    $scope.firstSelection = 2;
    $scope.secondSelection = 4;
});

Here is entire modifed code for your issue without ng-change: 这是没有ng-change的完整修改代码:

Html code : HTML代码:

<div ng-app="myapp">
    <fieldset ng-controller="myCtrl">

<select required  ng-model="reportData.car.make" ng-options="item for item in c" class="form-control" >
     <option value="">--Select--</option>
</select> 

<select required ng-model="selectedmodel" ng-options="source.name for source in sourceList" class="form-control" >
     <option value="">--Select--</option>
</select>

<select required ng-model="selectedYear" ng-options="item for item in selectedmodel.suboptions.yearList" class="form-control" >
    <option value="">--Select--</option>
</select>

<select required ng-model="selectedColor" ng-options="t.description for t in selectedmodel.suboptions.color" class="form-control" >
    <option value="">--Select--</option>
</select>

    </fieldset>
</div>

JS code: JS代码:

var myapp = angular.module('myapp', []);
myapp.controller('myCtrl', function ($scope) {


    $scope.c =['CADILLAC','CAR2'];
    $scope.models =['CTS','ATS'];
    $scope.sourceList = [
    {
       name: "CTS",
       suboptions: 
           { "yearList": [
          "2016",
          "2015",
          "2014"
        ],
        "color": [
          {"id":1, "hexvalue":"#000", "description":"Black"},
          {"id":2, "hexvalue":"#fff", "description":"White"},
          {"id":3, "hexvalue":"#FAF0BE", "description":"Blonde"},
          {"id":4, "hexvalue":"#0000FF", "description":"Blue"},
          {"id":5, "hexvalue":"#808080", "description":"Grey"}
        ]}       
    },
    {
    name: "ATS",
       suboptions: 
           { "yearList": [
          "2016",
          "2015",
          "2014"
        ],
        "color": [
          {"id":1, "hexvalue":"#000", "description":"Black"},
          {"id":2, "hexvalue":"#fff", "description":"White"},
          {"id":3, "hexvalue":"#FAF0BE", "description":"Blonde"},
          {"id":4, "hexvalue":"#0000FF", "description":"Blue"},
          {"id":5, "hexvalue":"#808080", "description":"Grey"}
        ]}
    }

];
});

Let me know if with above code also you are facing issue or not 让我知道您是否也遇到上述问题

I made this fiddle from your code. 我从您的代码中弄了这个小提琴。 I used ng-model values as parameters. 我使用ng-model值作为参数。

checkout this fiddle: https://jsfiddle.net/vh16yx5s/ 查看这个小提琴: https : //jsfiddle.net/vh16yx5s/

HTML Code HTML代码

    <div ng-app='test'>
  <div ng-controller="testCtrl">
    <select required ng-change="onCarChange(reportData.car)" ng-model="reportData.car" ng-options="bb.make for bb in cars" class="form-control">
      <option value="">--Select--</option>
    </select>

    <select required ng-model="reportData.car.model" ng-change="onModelChange(reportData.car.model)" ng-options="cha.name for cha in b.selectedModels" class="form-control">
      <option value="">--Select--</option>
    </select>

    <select required ng-model="reportData.car.year" ng-change="onYearChange(b,year)" ng-options="t for t in b.selectedYears" class="form-control">
      <option value="">--Select--</option>
    </select>

    <select required ng-model="reportData.car.color" ng-change="onColorChange(b,color)" ng-options="t.description for t in b.selectedColors" class="form-control">
      <option value="">--Select--</option>
    </select>
  </div>
</div>

AngularJS code AngularJS代码

var test = angular.module('test', []);

test.controller('testCtrl', function testCtrl($scope) {

   console.log('initiated');

   $scope.cars = [{
       "model": [{
         "year": ["2016","2015","2014"],
         "name": "CTS",
         "color": [{"id": 1, "hexvalue": "#000", "description": "Black" }, 
                             {"id": 2, "hexvalue": "#fff", "description": "White" },
                   {"id": 3, "hexvalue": "#FAF0BE", "description": "Blonde"},
                   {"id": 4, "hexvalue": "#0000FF", "description": "Blue"},
                   {"id": 5, "hexvalue": "#808080", "description": "Grey"}],
         "id": 0
       }, {
         "year": [
           "2016",
           "2015",
           "2014",
           "2013",
           "2012"
         ],
         "name": "ATS",
         "color": [{
           "id": 1,
           "hexvalue": "#000",
           "description": "Black"
         }, {
           "id": 2,
           "hexvalue": "#fff",
           "description": "White"
         }, {
           "id": 3,
           "hexvalue": "#FAF0BE",
           "description": "Blonde"
         }, {
           "id": 4,
           "hexvalue": "#0000FF",
           "description": "Blue"
         }, {
           "id": 5,
           "hexvalue": "#808080",
           "description": "Grey"
         }],
         "id": 1
       }, {
         "year": [
           "2016",
           "2015",
           "2014"
         ],
         "name": "XTS",
         "color": [{
           "id": 1,
           "hexvalue": "#000",
           "description": "Black"
         }, {
           "id": 2,
           "hexvalue": "#fff",
           "description": "White"
         }, {
           "id": 3,
           "hexvalue": "#FAF0BE",
           "description": "Blonde"
         }, {
           "id": 4,
           "hexvalue": "#0000FF",
           "description": "Blue"
         }],
         "id": 2
       }],
       "make": "CADILLAC",
       "picture": "http://placehold.it/32x32",
       "index": 0,
       "_id": "573bef46573891a64081d4d6"
     }
   ];

   $scope.b = {};

   $scope.onCarChange = function(car) {
     console.log(car);
     $scope.b.selectedModels = car.model;
   }

   $scope.onModelChange = function(model) {
   console.log(model);
     $scope.b.selectedModel = model;
     $scope.b.selectedYears = model.year;
     $scope.b.selectedColors = model.color;
   }

   $scope.onYearChange = function(b, year) {
     $scope.b.selectedYear = year;
     $scope.b.selectedColor = years.colors;
   }

   $scope.onColorChange = function(b, color) {
     $scope.b.selectedColor = color;
   }

 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM