javascript - 我可以填充 ServiceType,但无法获取 ServiceName 的正确值(相关下拉列表)

标签 javascript angularjs

如何根据 ServiceType 下拉列表选择的值获得正确的 ServiceName 下拉值。

$scope.ServiceTypeAndName = [    
    { 
      "id":0,
      "type":"",
      "list":""
    },
     { 
      "id":1,
      "type":"In-Person",
      "list":["ADL Functional Assessment", "Community Functional Assessment", "Future Care Cost Analysis"]
    },
     { 
      "id":2,
      "type":"Paper Review",
      "list":["OT - In Home Assessment - ABI", "OT - In Home Assessment Attendant Care with Form 1", "OT - Occupational Therapy Assessment"]
          
    },
     { 
      "id":3,
      "type":"Administration",
      "list":["Situational Assessment", "Situational Assessment (OT) - Day 1", "Situational Assessment (OT) - Day 2", "Construction Cost Consulting"]
    }  
];


$scope.selectedLine = [{"Event_ID": "100", "Service_Type": "In-Person", "Service_Name": "Community Functional Assessment"}, {"Event_ID": "101", "Service_Type": "Paper Review", "Service_Name": "OT - Occupational Therapy Assessment"},{"Event_ID": "102", "Service_Type": "In-Person", "Service_Name": "Future Care Cost Analysis"}];
<select ng-model="selectedLine.Service_Type" name="ServiceType" class="form-control">
   <option ng-repeat="temp in ServiceTypeAndName" value="{{temp.type}}">{{temp.type}}</option>
</select>
										
<select ng-model="selectedLine.Service_Name" name="ServiceName" ng-required="selectedLine.Service_Type!=''" class="form-control"> 
  
</select>

例如,如果我加载第一个对象,则应在第一个下拉列表中选择值“In-Person”,并应在第二个下拉列表中选择值“社区功能评估”以及相应的列表值,并且如果我更改第一个下拉列表,然后应在第二个下拉列表中填充相应的列表值。

最佳答案

您的数据的结构方式并不是最简单的,但它仍然不是最糟糕的:

Working Plnkr

基本上,您需要找出您匹配的 ServiceTypeAndName 数组中的哪个对象。这需要一个过滤器:

$scope.selectedLineChanged = function () {                  
   $scope.SelectedServiceTypeAndName = $filter('filter')($scope.ServiceTypeAndName, $scope.selectedLine.Service_Type)[0];           
};  

事情是这样的:我不知道您打算如何加载所选项目,即存储在 selectedLines 数组中的项目。我通过该 plnkr 中的选择框加载项目。

当顶部框更改时,它与其余两个框匹配。

enter image description here

关于javascript - 我可以填充 ServiceType,但无法获取 ServiceName 的正确值(相关下拉列表),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38572368/

相关文章:

javascript - 将 ng-click 值传递给一个 Controller 并在另一个 Controller 中使用它

javascript - 如何在 contenteditable 元素(div)中设置插入符号(光标)位置?

javascript - jQuery : Want to disable all the input fields if editqastatus value = 1 or 2

javascript - ng-change 事件不适用于下拉菜单

javascript - Angular Directive(指令)和同位素

javascript - angular.foreach 在发送 get 请求时给我重复的数据

javascript - AngularJS > ui.router 仅在数据准备好时使用 promise 。

Javascript 窗口调整大小在 Safari 中不起作用,但在 Firefox 中起作用

javascript - 如何从 IE 列表中删除 list it (li) 元素

javascript - 网络组件测试仪因自耕农生成器 polymer 而失败