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

javascript - Angular issue when ng-if is 0 - Stack Overflow

programmeradmin2浏览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 :

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

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

Share Improve this question asked Jan 23, 2017 at 19:42 Deepak BandiDeepak Bandi 1,9044 gold badges22 silver badges37 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

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 parison, 0='', but 1='1')

Change

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

to

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

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

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

To the question of paring 0, you can do it by putting strictly parison, 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

发布评论

评论列表(0)

  1. 暂无评论