最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to display one element if condition with Angular JS - Stack Overflow

programmeradmin0浏览0评论

I want display a select box if unity_variable equals true or just the unity if not unity_variable equals false

I ve try this :

<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>

And this :

<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>

so if i didn't put the ng-show that make for each question a select box with unity or display the twice but not respect the condition... Please help to find my mistake !

EDIT :

My Json look like this for displaying controls to build a form :

{"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"}
        ]
}

My html code is :

<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>

if i try just the simply code :

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

The result show me true and false so i ve a syntaxe error

I want display a select box if unity_variable equals true or just the unity if not unity_variable equals false

I ve try this :

<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>

And this :

<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>

so if i didn't put the ng-show that make for each question a select box with unity or display the twice but not respect the condition... Please help to find my mistake !

EDIT :

My Json look like this for displaying controls to build a form :

{"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"}
        ]
}

My html code is :

<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>

if i try just the simply code :

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

The result show me true and false so i ve a syntaxe error

Share Improve this question edited Sep 12, 2013 at 12:33 Ema.H asked Sep 12, 2013 at 12:02 Ema.HEma.H 2,8783 gold badges29 silver badges41 bronze badges 6
  • 2 for ngIf make sure you are using atleast version 1.5 – AlwaysALearner Commented Sep 12, 2013 at 12:05
  • yes i m in AngularJS v1.0.7, i try it but in the offical website the last version is AngularJS v1.0.8... Thx ;) – Ema.H Commented Sep 12, 2013 at 12:06
  • ... not better, the result is the same – Ema.H Commented Sep 12, 2013 at 12:09
  • 1 Can you create a fiddle? Sorry..I meant version 1.1.5. You can download the newer versions from here: code.angularjs.org – AlwaysALearner Commented Sep 12, 2013 at 12:10
  • Difficult because all data are from a json file etc... but i try to do something near ^^ – Ema.H Commented Sep 12, 2013 at 12:16
 |  Show 1 more comment

2 Answers 2

Reset to default 13

This works for me:

<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>

As of 8/6/2013 you must use the Angular.js "unstable" version 1.1.5 to use ngIf. If you started with angular-seed it uses the "stable" version 1.0.7 which does not support ngIf.

发布评论

评论列表(0)

  1. 暂无评论