如果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", "dbcolumn":"confidential data", "control": "input", "default_value": "", "unity": "", "isnull": 1, "size":"180px", "data_type":"number", "max_length": 2},
{"name": "confidential data", "dbcolumn":"confidential data","control": "select", "default_value": [{"name":"Femme", "value": "Femme"}, {"name":"Homme", "value": "Homme"}], "unity": "", "isnull": 0, "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>
结果显示我是真假,所以我有一个语法错误
答案 0 :(得分:13)
这对我有用:
<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>
答案 1 :(得分:0)
从2013年8月6日起,您必须使用Angular.js“不稳定”版本1.1.5才能使用ngIf。如果你从angular-seed开始,它使用“稳定”版本1.0.7,它不支持ngIf。