Deepak Bandi
Deepak Bandi

Reputation: 1904

Angular issue when ng-if is 0

I have a issue when the ng-if="expression value is 0" it does no work as it is believed to be.

Here is the link to the plunker : http://plnkr.co/edit/dILXSIHHzvuv1nhbxdga?p=info

On change of select, I'm using the model value to show particular text box.

<select ng-model="location.locationSchedule.scheduleType" 
    ng-options="schedule.id as schedule.name for schedule in scheduleTypes track by schedule.id">
   <option value="">Choose a Schedule Type</option>
</select>    

And the input field

<input ng-model="location.locationSchedule.frequency" 
    placeholder="Enter Week No." 
    ng-if="location.locationSchedule.scheduleType == 0"/>
<input ng-model="location.locationSchedule.frequency" 
    placeholder="Enter Week No." 
    ng-if="location.locationSchedule.scheduleType == 1"/>

If the location.locationSchedule.scheduleType == 1 it displays the particular textbox, but not when it is 0

Upvotes: 3

Views: 11394

Answers (3)

Anthony C
Anthony C

Reputation: 2157

Not sure why everyone is comparing scheduleType !== '' Now I see why, please include the div html in your question for clarity.

To the question of comparing 0, you can do it by putting strictly comparison, including !== '', === 0, etc

<div class="form-group days-list" ng-if="location.locationSchedule.scheduleType !== ''">
    <input ng-model="location.locationSchedule.frequency" 
        placeholder="Enter Week No." 
        ng-if="location.locationSchedule.scheduleType === 0"/>
    <input ng-model="location.locationSchedule.frequency" 
        placeholder="Enter Week No." 
        ng-if="location.locationSchedule.scheduleType === 1"/>

http://plnkr.co/edit/JWBz4QlhLDHrNiMo36zI?p=preview

Upvotes: 1

MeTe-30
MeTe-30

Reputation: 2542

Change

ng-if="location.locationSchedule.scheduleType != ''"

to

ng-if="location.locationSchedule.scheduleType !== ''"

beacause 0 == '', both are falsy, but their types are not the same.

Upvotes: 1

ValLeNain
ValLeNain

Reputation: 2304

That's because of the parent ngIf

Replace

ng-if="location.locationSchedule.scheduleType != ''"

with

ng-if="location.locationSchedule.scheduleType !== ''"

Or anything more suitable (cf Implied string comparison, 0='', but 1='1')

Upvotes: 2

Related Questions