根据<select>标签

时间:2017-02-24 13:56:13

标签: javascript angularjs show-hide ng-show angularjs-ng-change

更新:对不起,我应该更清楚,但我想要的也是如果我从&#34;下拉1&#34;除了空值之外,我的表2自动消失b / c我只想在我使用下拉菜单时看到表1.对于&#34;下拉2&#34;同样的事情。如果我从下拉列表2中选择任何非空值,我的表1就会消失,我只会看到表2.我还在这里更新了我的小提琴https://jsfiddle.net/missggnyc/4vod1L9g/7/并且表格隐藏了从下拉列表中选择空值。我现在需要的是如何根据我使用的下拉列表显示一个或另一个表。 原始邮寄: 我对如何使用ng-show或ng-change来显示我的两个不同的表格感到有点困惑,这取决于用户选择有两个dif。堕落。 这是一个场景: 只要用户在两个下拉列表中都没有选择空值的SELECT YOUR ANSWER,我就会有一个过滤器来进行字符串比较并按颜色过滤掉。这就是我想要做的。 用户选择&#34;下拉1&#34; 如果用户没有选择&#34;选择你的答案&#34;具有空值 如果用户选择&#34;选择你的答案&#34;,则不显示任何表格 隐藏表2,只要&#34;下拉1&#34;正在被选中 用户选择&#34;下拉2&#34; 如果用户没有选择&#34;选择您的答案&#34;具有空值 如果用户选择&#34;选择你的答案&#34;,则不显示任何表格 隐藏表2,只要&#34;下拉1&#34;正在被选中 我很困惑如何在我的情况下使用ng-show或ng-change。有什么建议? 在这里看演示 HTML &lt; div ng-app =&#34; myColors&#34;&gt;   &lt; div ng-controller =&#34; cController&#34;&gt;     &lt; h3&gt;下拉1&lt; / h3&gt;     &lt; select ng-model =&#34; selectedAnswer1&#34; ng-options =&#34; c.cat for mySelection1&#34; NG-变化=&#34;&#34;&GT;&LT; /选择&GT;     &LT;预&GT; {{selectedAnswer1}}&LT; /预&GT;        &lt; h3&gt;下拉2&lt; / h3&gt;     &lt; select ng-model =&#34; selectedAnswer2&#34; ng-options =&#34; c.cat for mySelection2&#34;&gt;&lt; / select&gt;     &LT;预&GT; {{selectedAnswer2}}&LT; /预&GT;     &LT; HR&GT;     &LT; H4&GT;      表格1     &LT; / H4&GT;     &LT;表&gt;       &LT; TR&GT;         &LT; TD&GT; ID&LT; / TD&GT;         &LT; TD&GT;分类&LT; / TD&GT;       &LT; / TR&GT;       &lt; tr ng-repeat =&#34; f in fruit&#34;&gt;         &LT; TD&GT; {{f.id}}&LT; / TD&GT;         &LT; TD&GT; {{f.f_category}}&LT; / TD&GT;       &LT; / TR&GT;     &LT; /表&gt;     &LT; H4&GT;       表2     &LT; / H4&GT;     &LT;表&gt;       &LT; TR&GT;         &LT; TD&GT;汽车和LT; / TD&GT;       &LT; / TR&GT;        &lt; tr ng-repeat =&#34; c in car&#34;&gt;         &LT; TD&GT; {{c.category}}&LT; / TD&GT;       &LT; / TR&GT;     &LT; /表&gt;   &LT; / DIV&GT; &LT; / DIV&GT; JS var app = angular.module(&#34; myColors&#34;,[]); app.controller(&#34; cController&#34;,function($ scope){     $ scope.mySelection1 = [     {&#34; cat&#34;:&#34;选择你的答案&#34;,&#34;价值&#34;:null},     {&#34; cat&#34;:&#34; YELLOW&#34;,&#34; value&#34;:&#34; yellow&#34;},     {&#34; cat&#34;:&#34; ORANGE&#34;,&#34; value&#34;:&#34; orange&#34;}   ]。   $ scope.mySelection2 = [     {&#34; cat&#34;:&#34;选择你的答案&#34;,&#34;价值&#34;:null},     {&#34; cat&#34;:&#34; GREEN CAR&#34;,&#34; value&#34;:&#34; green&#34;},     {&#34; cat&#34;:&#34; BLUE CAR&#34;,&#34; value&#34;:&#34; blue&#34;}   ]。   $ scope.fruit = [{     &#34; id&#34;:&#34; red&#34;,     &#34; f_category&#34;:[&#34; Apple&#34;,&#34; Strawberry&#34;,&#34; Pineapple&#34;]   },{     &#34; id&#34;:&#34;黄色&#34;,     &#34; f_category&#34;:[&#34; Banana&#34;,&#34; Pineapple&#34;]   },{     &#34; id&#34;:&#34; orange&#34;,     &#34; f_category&#34;:[&#34; Peach&#34;,&#34; Nectarine&#34;]   }];   $ scope.car = [{     &#34; name&#34;:&#34; yellow&#34;,     &#34;类别&#34;:[&#34; SUV&#34;,&#34;卡车&#34;]   },{     &#34; name&#34;:&#34; blue&#34;,     &#34;类别&#34;:[&#34; Van&#34;]   },{     &#34; name&#34;:&#34; orange&#34;,     &#34;类别&#34;:[&#34; Mini-Van&#34;]   }]; });

3 个答案:

答案 0 :(得分:0)

ng-show允许您利用数据绑定,因此您不必像在vanilla JS中那样手动监视事件(使用事件处理程序或onchange属性)。这是在angularJs中做事的首选方式。只有在没有其他选择时才使用ngChange。

此外,在这里使用ng-if可能更好,因为如果不显示,您将节省构建表的成本。

答案 1 :(得分:0)

使用ng-changefilter指令来实现您期望的行为,请查看此代码段

var app = angular.module("myColors", []);
app.controller("cController", function($scope) {
	$scope.selectedFruit = null;
	$scope.selectedCar = null;
	$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.selectFruit = function(selectedItem){
		$scope.selectedFruit = selectedItem.value;	
	}	
	$scope.selectCar = function(selectedItem){
		$scope.selectedCar = selectedItem.value;	
	}
  
});
table, th, td {
   border: 1px solid black;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.min.js"></script>


<div ng-app="myColors">
  <div ng-controller="cController">
    <h3>drop down 1</h3>
    <select ng-model="selectedAnswer1" ng-options="c.cat for c in mySelection1" ng-change="selectFruit(selectedAnswer1)"></select>
    <pre>{{selectedAnswer1}}</pre>
       <h3>drop down 2</h3>
    <select ng-model="selectedAnswer2" ng-options="c.cat for c in mySelection2" ng-change="selectCar(selectedAnswer2)"></select>
    <pre>{{selectedAnswer2}}</pre>
    <hr>
    <h4>
     table 1
    </h4>
    <table>
      <tr>
        <td>ID</td>
        <td>Category</td>       
      </tr>
      <tr ng-repeat="f in fruit | filter:selectedFruit">
        <td>{{f.id}}</td>
        <td>{{f.f_category}}</td>        
      </tr>
    </table>
    
    <h4>
      table 2
    </h4>
    <table>
      <tr>
        <td>Car</td>
      </tr>
       <tr ng-repeat="c in car | filter:selectedCar">        
        <td>{{c.category}}</td>        
      </tr>
    </table>
    
  </div>
</div>

答案 2 :(得分:0)

更新: 据我所知,如果选择第一个表,你只想显示/隐藏另一个表。这是一个简单的解决方案:

 <div ng-app="myColors">
  <div ng-controller="cController">
    <h3>drop down 1</h3>
    <select name="select1" ng-model="selectedAnswer1" ng-options="c.cat for c in mySelection1" ng-change="selection1Change()"></select>
    <pre>{{selectedAnswer1}}</pre>
       <h3>drop down 2</h3>
    <select name="select2" ng-model="selectedAnswer2" ng-options="c.cat for c in mySelection2" ng-change="selection2Change()"></select>
    <pre>{{selectedAnswer2}}</pre>
    <hr>
    <div ng-show="selectedAnswer1 && flag1">
    <h4>
     table 1
    </h4>
    <table>
      <tr>
        <td>ID</td>
        <td>Category</td>       
      </tr>
      <tr ng-repeat="f in fruit" ng-show="selectedAnswer1.value === f.id">
        <div >
        <td >{{f.id}}</td>
        <td >{{f.f_category}}</td> 
        </div>
      </tr>
    </table>
    </div>
    <div  ng-show="selectedAnswer2 && flag2">
    <h4>
      table 2
    </h4>
    <table>
      <tr>
        <td>Car</td>
      </tr>
       <tr ng-repeat="c in car" ng-show="selectedAnswer2.value === c.name">        
        <td>{{c.category}}</td>        
      </tr>
    </table>

    </div>
  </div>
</div>
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;
  }
});

OLD: 不需要使用ng-change,因为angularjs本身会更新ng-model的值,所以你可以使用ng-show和ng-model的值,如下面的代码所示:

<div ng-app="myColors">
  <div ng-controller="cController">
    <h3>drop down 1</h3>
    <select ng-model="selectedAnswer1" ng-options="c.cat for c in mySelection1"></select>
    <pre>{{selectedAnswer1}}</pre>
       <h3>drop down 2</h3>
    <select ng-model="selectedAnswer2" ng-options="c.cat for c in mySelection2"></select>
    <pre>{{selectedAnswer2}}</pre>
    <hr>
    <h4>
     table 1
    </h4>
    <table ng-show="selectedAnswer1">
      <tr>
        <td>ID</td>
        <td>Category</td>       
      </tr>
      <tr ng-repeat="f in fruit" ng-show="selectedAnswer1.value === f.id">
        <div >
        <td >{{f.id}}</td>
        <td >{{f.f_category}}</td> 
        </div>
      </tr>
    </table>

    <h4>
      table 2
    </h4>
    <table ng-show="selectedAnswer2">
      <tr>
        <td>Car</td>
      </tr>
       <tr ng-repeat="c in car" ng-show="selectedAnswer2.value === c.name">        
        <td>{{c.category}}</td>        
      </tr>
    </table>

  </div>
</div>