Check if Array is empty Ng-Switch

Asked

Viewed 480 times

1

I check a parameter with an array that may have values it or not. I have an Ng-Switch does a check on this array. I wish that when it had no value show a Message.

I got to do the ng-switch-when=""but it didn’t work and I also don’t know if it’s right.

<div ng-repeat="p in ctrl.currentreport.parameters" ng-switch="p.dataType">
                        <div ng-switch-when="">
                            Não há dados
                        </div>
                        <div ng-switch-when="Integer">
                            <reportinteger parameter="p"></reportinteger>
                        </div>
                        <div ng-switch-when="DateTime">
                            <reportdate parameter="p"></reportdate>
                        </div>
                        <div ng-switch-when="Boolean">
                            <reportboolean parameter="p"></reportboolean>
                        </div>
                    </div>

2 answers

1

Use

<div ng-switch-default>

           Alguma coisa...   
 </div>
  • Good morning, Thavia did not work. The Parameters object will always exist, which may or may not be the data contained in it. And this selection I make by dateType. Would have some other idea?

  • ng-switch-default works when the value you are checking does not match with any ng-switch-when... How is the p.dataType object?

  • Inside the "Parameters" I have "dataType": "Integer". but I also have "Parameters":"{}".

  • I get it, how can it be that you don’t have the datatype for ng-swicth to check, so it won’t even work. You must place your ng-switch inside a <div ng-if=''p.dataType"> <div ng-switch-when="Integer"> <reportinteger Parameter="p"></reportinteger> </div>... </div> and in another div Oce puts the situation counter <div ng-if='! p.dataType"> Something</div>

  • It didn’t work, the problem is that it’s not entering the <div ng-repeat> which is where I put ng-if. I’ll put the code below

  • I did a <ng-if="Ctrl.currentreport.Parameters>...</div> test. But when I put <ng-if="Ctrl.currentreport.Parameters.dataType>...</div> it doesn’t run even having dataType inside the Parameters.

  • Your Ctrl.currentreport.Parameters is an array of objects, so it doesn’t find dataType, so ng-if has to be in the div within ng-repeat. Make a console.log of your object Ctrl.currentreport.Parameters, send it to me q help you in a fiddle.

  • How would I make this console.log? Do you want to know what is contained in it?

  • <div ng-repeat="t in Ctrl.currentreport.Parameters">{t}</dv> {"name":"Kminicio","Description":"Km Inicio","dataType":"Integer","defaultValue":null,"value":null,"required":true,"validValues":{}} {"name":"KmFim","description":"Km Fim","dataType":"Integer","defaultValue":null,"value":null,"required":true,"validValues":{}}&#xA;{"name":"DataInicio","description":"Data Inicio","dataType":"DateTime","defaultValue":null,"value":null,"required":true,"validValues":{}}&#xA;{"name":"Datatermino","Description":"Data Termino","dataType":"Datetime","defaultValue":null,"value":null,"required":true,"validValues":{}}

Show 4 more comments

1

<div ng-repeat="p in ctrl.currentreport.parameters">
                        <div ng-if="!p.dataType">
                            Não tem dados
                        </div>
                        <div ng-if="p.dataType">
                            <div ng-switch="p.dataType">
                                <div ng-switch-when="Integer">
                                    <reportinteger parameter="p"></reportinteger>
                                </div>
                                <div ng-switch-when="DateTime">
                                    <reportdate parameter="p"></reportdate>
                                </div>
                                <div ng-switch-when="Boolean">
                                    <reportboolean parameter="p"></reportboolean>
                                </div>
                                <div ng-switch-when="String">
                                    <reportstring parameter="p"></reportstring>
                                </div>
                            </div>
                        </div>
                    </div>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.