简体   繁体   English

如何使用ng-if或ng-show使md-select移除(隐藏)?

[英]How to use ng-if or ng-show to make md-select removed(hide)?

I am trying to make a md-select removed or hide when a checkbox is not checked. 我试图将md-select删除或在未选中复选框时隐藏。 I have tired both ng-if or ng-show, but once the md-select is displayed, it cannot be removed or hide. 我对ng-if或ng-show都感到厌倦,但是一旦显示md-select,就无法将其删除或隐藏。 My code is as follows: 我的代码如下:

Using ng-if: 使用ng-if:

<md-checkbox ng-model="checked" ng-true-value="'true'" ng-false-value="'false'">
Add Dependency
</md-checkbox>

<p>Checkbox value: {{ checked }}</p>
<md-select  placeholder="Dependency" ng-model="test" style="min-width: 300px;" ng-if ="checked">
<md-option>testOption1</md-option>
<md-option>testOption2</md-option>
<md-option>testOption3</md-option>
</md-select>

Using ng-show 使用ng-show

<md-checkbox ng-model="checked" ng-true-value="'true'" ng-false-value="'false'">
Add Dependency
</md-checkbox>

<p>Checkbox value: {{ checked }}</p>
<md-select  placeholder="Dependency" ng-model="test" style="min-width: 300px;" ng-show ="checked">
<md-option>testOption1</md-option>
<md-option>testOption2</md-option>
<md-option>testOption3</md-option>
</md-select>

Where did I do wrong? 我在哪里做错了? Thank you. 谢谢。

This is because you have "ng-true-value" and "ng-false-value" as true/false strings not bools. 这是因为您将“ ng-true-value”和“ ng-false-value”作为true / false字符串而不是布尔值。 You can fix this by either... 您可以通过以下任一方法解决此问题:

Change ng-true-value="'true'" to ng-true-value="true" 将ng-true-value =“'true'”更改为ng-true-value =“ true”

 angular.module('sandbox', ['ngMaterial']); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.6/angular.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.6/angular-aria.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.6/angular-animate.js"></script> <script src="https://rawgit.com/angular/bower-material/master/angular-material.js"></script> <link href="https://rawgit.com/angular/bower-material/master/angular-material.css" rel="stylesheet"/> <div ng-app="sandbox"> <md-checkbox ng-model="checked" ng-true-value="true" ng-false-value="false"> Add Dependency </md-checkbox> <p>Checkbox value: {{ checked }}</p> <md-select placeholder="Dependency" ng-model="test" style="min-width: 300px;" ng-show ="checked"> <md-option>testOption1</md-option> <md-option>testOption2</md-option> <md-option>testOption3</md-option> </md-select> </div> 

OR 要么

Change ng-show="checked" to ng-show="checked === 'true'" 将ng-show =“ checked”更改为ng-show =“ checked ==='true'”

 angular.module('sandbox', ['ngMaterial']); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.6/angular.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.6/angular-aria.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.6/angular-animate.js"></script> <script src="https://rawgit.com/angular/bower-material/master/angular-material.js"></script> <link href="https://rawgit.com/angular/bower-material/master/angular-material.css" rel="stylesheet"/> <div ng-app="sandbox"> <md-checkbox ng-model="checked" ng-true-value="'true'" ng-false-value="'false'"> Add Dependency </md-checkbox> <p>Checkbox value: {{ checked }}</p> <md-select placeholder="Dependency" ng-model="test" style="min-width: 300px;" ng-show ="checked === 'true'"> <md-option>testOption1</md-option> <md-option>testOption2</md-option> <md-option>testOption3</md-option> </md-select> </div> 

You are passing to md-checkbox a true and false value which are 'true' (string) and 'false' (string). 您向md-checkbox传递了true和false值,分别是'true'(字符串)和'false'(字符串)。 So when you write ng-if="checked" or ng-show="checked", angular is just checking your "checked" string variable is not undefined. 因此,当您编写ng-if =“ checked”或ng-show =“ checked”时,angular只是在检查您的“ checked”字符串变量是否未定义。

Try to remove this : 尝试删除此:

ng-true-value="'true'" ng-false-value="'false'"

Make it simple, ng-model is enough to handle this. 简单来说, ng-model就足以解决这个问题。 No need of ng-true-value\\ng-false-value , It's generally required when you need custom flag like Yes\\No 不需要ng-true-value\\ng-false-value ,通常在需要像Yes\\No这样的自定义标志时需要

So : <md-checkbox ng-model="checked"> will do it for you. 因此: <md-checkbox ng-model="checked">将为您完成。

See this working fiddle 看到这个工作的小提琴

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

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