简体   繁体   English

ng-if为0时的角度问题

[英]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. ng-if="expression value is 0"时,我遇到了问题,因为它被认为是行不通的。

Here is the link to the plunker : http://plnkr.co/edit/dILXSIHHzvuv1nhbxdga?p=info 这是到pl车的链接: 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 如果location.locationSchedule.scheduleType == 1则显示特定的文本框,但当它为0时不显示

That's because of the parent ngIf 那是因为父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' ) 或更合适的方法(参见隐式字符串比较,0 ='',但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. beacause 0 == '' ,两者都是虚假的,但是它们的类型不同。

Not sure why everyone is comparing scheduleType !== '' Now I see why, please include the div html in your question for clarity. 不确定为什么每个人都在比较 scheduleType !== '' 现在我知道为什么了,为了清楚起见,请在您的问题中包括div html。

To the question of comparing 0, you can do it by putting strictly comparison, including !== '' , === 0 , etc 对于比较0的问题,可以通过严格比较来做到,包括!== ''=== 0等等

<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 http://plnkr.co/edit/JWBz4QlhLDHrNiMo36zI?p=preview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM