javascript – 如果条件使用Angular JS,如何显示一个元素

前端之家收集整理的这篇文章主要介绍了javascript – 如果条件使用Angular JS,如何显示一个元素前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如果unity_variable等于true,我想显示一个选择框,如果不是unity_variable等于false,我想要显示单位

我试过这个:

<div ng-if="question.unity_variable == true">
     <select>
          <option data-ng-repeat="unit in question.unity">{{unit.value}}</option>
     </select>
</div>
<div ng-if="question.unity_variable ==  false || question.unity_variable == ''">
     {{question.unity}}
</div>

还有这个 :

<div ng-show="question.unity_variable == true" ng-hide="question.unity_variable ==  false || question.unity_variable == ''">
     <select>
          <option data-ng-repeat="unit in question.unity">{{unit.value}}</option>
     </select>
</div>
<div ng-show="question.unity_variable ==  false || question.unity_variable == ''">
     {{question.unity}}
</div>

所以,如果我没有把每个问题的ng-show用于统一的选择框或显示两次但不尊重条件……
请帮忙找出我的错误

编辑:

我的Json看起来像这样显示控件来构建表单:

{"id": "01","questions": [
        {"name": "confidential data","dbcolumn":"confidential data","control": "input","default_value": "","unity": [{"value": "mmol/l"},{"value": "autre"}],"unity_variable": true,"isnull": 0,"size":"180px","data_type":"number","max_length": 3},{"name": "confidential data","unity": "","isnull": 1,"max_length": 2},"control": "select","default_value": [{"name":"Femme","value": "Femme"},{"name":"Homme","value": "Homme"}],"size":"196px","data_type":"string"}
        ]
}

我的HTML代码是:

<table style="display: inline-block; float: left; max-width: 510px;">
    <tr data-ng-repeat="question in group.questions">
        <td>{{question.name}}</td>
        <td>
            <div ng-switch on="question.control" style="display: inline-block;">
                 <div ng-switch-when="input">
                     <input type="text" style="width: {{ question.size }};" data-isnull="{{question.isnull}}" name="{{question.dbcolumn}}" id="{{question.dbcolumn}}" placeholder="{{question.default_value}}" maxlength="{{question.max_length}}" />
                 </div>
                 <div ng-switch-when="textarea">
                      <textarea style="width: {{ question.size }};" data-isnull="{{question.isnull}}" name="{{question.dbcolumn}}" id="{{question.dbcolumn}}" placeholder="{{question.default_value}}"></textarea>
                 </div>
                 <div ng-switch-when="checkBox">
                      <input type="checkBox" data-isnull="{{question.isnull}}" name="{{question.dbcolumn}}" id="{{question.dbcolumn}}" />{{question.default_value}}
                 </div>
                 <div ng-switch-when="select">
                      <select style="width: {{ question.size }};" data-isnull="{{question.isnull}}" name="{{question.dbcolumn}}" id="{{question.dbcolumn}}">
                          <option data-ng-repeat="choice in question.default_value" value="{{choice.value}}">{{choice.name}}</option>
                      </select>
                 </div>
                 <div ng-switch-when="p">
                      <p style="width: {{ question.size }};" data-isnull="{{question.isnull}}" name="{{question.dbcolumn}}" id="{{question.dbcolumn}}"><b>{{question.default_value}}</b></p>
                </div>
                <div ng-switch-default>
                      <!-- default action -->
                </div>
            </div>
        </td>
        <td>
            <div ng-if="question.unity_variable == true">
                 <select>
                      <option data-ng-repeat="unit in question.unity">{{unit.value}}</option>
                 </select>
            </div>
            <div ng-if="question.unity_variable ==  false || question.unity_variable == ''"> {{question.unity}} </div>    
       </td>
    </tr>

如果我只尝试简单的代码

<div ng-if="1 == 1">true</div>
<div ng-if="1 == 2">false</div>

结果显示我是真假,所以我有一个语法错误

解决方法

这对我有用:
<div ng-if="question.unity_variable">
    <select>
        <option data-ng-repeat="unit in question.unity">{{unit.value}}</option>
    </select>
</div>
<div ng-if="!question.unity_variable">
    {{question.unity}}
</div>

猜你在找的JavaScript相关文章