Angular ng-show and ng-change to show and hide tables depending on the user selection in <select> tag
更新:不好意思,我应该说得更清楚,但我想要的也是,如果我从"下拉1"中选择,而不是从
原始职位:我对如何使用
下面是场景:
只要用户在两个下拉列表中都没有选择
用户从"下拉1"中选择
- 如果用户未选择空值为"选择您的答案",则显示带筛选结果的表1
- 如果用户选择"选择您的答案",则不显示表格。
- 只要选择了"下拉1",就隐藏表2
用户从"下拉2"中选择
- 如果用户没有选择空值为"选择您的答案",则显示带有筛选结果的表2
- 如果用户选择"选择您的答案",则不显示表格。
- 只要选择了"下拉1",就隐藏表2
我对如何使用ng-show或ng-change感到困惑。有什么建议吗?
在这里看到演示
HTML
1 2 3 | drop down 1 <select ng-model="selectedAnswer1" ng-options="c.cat for c in mySelection1" ng-change=""></select> [cc lang="javascript"]{{selectedAnswer1}} |
下拉2
1 | {{selectedAnswer2}} |
表1
<表>表2
<表>JS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | var app = angular.module("myColors", []); app.controller("cController", function($scope) { $scope.mySelection1 = [ {"cat":"SELECT YOUR ANSWER","value": null}, {"cat":"YELLOW", "value":"yellow"}, {"cat":"ORANGE","value":"orange"} ]; $scope.mySelection2 = [ {"cat":"SELECT YOUR ANSWER", "value": null }, {"cat":"GREEN CAR", "value":"green"}, {"cat":"BLUE CAR","value":"blue"} ]; $scope.fruit = [{ "id":"red", "f_category": ["Apple","Strawberry","Pineapple"] }, { "id":"yellow", "f_category": ["Banana","Pineapple"] }, { "id":"orange", "f_category": ["Peach","Nectarine"] }]; $scope.car = [{ "name":"yellow", "category": ["SUV","Truck"] }, { "name":"blue", "category": ["Van"] }, { "name":"orange", "category": ["Mini-Van"] }]; }); |
UPDATE:
as far I understood you only want show/hide the other table if the first one is select. Here is a simple solution:
1 2 3 | drop down 1 <select name="select1" ng-model="selectedAnswer1" ng-options="c.cat for c in mySelection1" ng-change="selection1Change()"></select> [cc lang="javascript"]{{selectedAnswer1}} |
下拉2
1 | {{selectedAnswer2}} |
表1
<表>表2
<表>1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | var app = angular.module("myColors", []); app.controller("cController", function($scope) { $scope.mySelection1 = [ {"cat":"SELECT YOUR ANSWER","value": null}, {"cat":"YELLOW", "value":"yellow"}, {"cat":"ORANGE","value":"orange"} ]; $scope.mySelection2 = [ {"cat":"SELECT YOUR ANSWER", "value": null }, {"cat":"GREEN CAR", "value":"green"}, {"cat":"BLUE CAR","value":"blue"} ]; $scope.fruit = [{ "id":"red", "f_category": ["Apple","Strawberry","Pineapple"] }, { "id":"yellow", "f_category": ["Banana","Pineapple"] }, { "id":"orange", "f_category": ["Peach","Nectarine"] }]; $scope.car = [{ "name":"yellow", "category": ["SUV","Truck"] }, { "name":"blue", "category": ["Van"] }, { "name":"orange", "category": ["Mini-Van"] }]; $scope.selection1Change = function(){ $scope.flag1 = true; $scope.flag2 = false; } $scope.selection2Change = function(){ $scope.flag1 = false; $scope.flag2 = true; } }); |
旧的:不需要使用ng change,因为angularjs本身会更新ng model的值,所以可以使用ng show和ng model的值,如下代码所示:
1 2 3 | drop down 1 <select ng-model="selectedAnswer1" ng-options="c.cat for c in mySelection1"></select> [cc lang="javascript"]{{selectedAnswer1}} |
下拉2
1 | {{selectedAnswer2}} |
表1
ID | 类别 |
F.ID | {{f.f_category}} |
C.类别 |