Search code examples
javascriptangularjsangular-ng-if

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


Solution

  • 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')