Angular ng-if 为 0 时的问题
Angular issue when ng-if is 0
我遇到了一个问题,ng-if="expression value is 0"
它无法正常工作。
这里是 link 给 plunker 的消息:http://plnkr.co/edit/dILXSIHHzvuv1nhbxdga?p=info
在更改 select 时,我使用模型值来显示特定的文本框。
<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>
和输入字段
<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"/>
如果 location.locationSchedule.scheduleType == 1
它显示特定的文本框,但当它为 0 时不显示
那是因为父 ngIf
替换
ng-if="location.locationSchedule.scheduleType != ''"
和
ng-if="location.locationSchedule.scheduleType !== ''"
或者更合适的(cf Implied string comparison, 0='', but 1='1')
改变
ng-if="location.locationSchedule.scheduleType != ''"
至
ng-if="location.locationSchedule.scheduleType !== ''"
因为0 == ''
,两者都是假的,但它们的类型不一样。
不知道为什么每个人都在比较scheduleType !== ''
现在我明白为什么了,为了清楚起见,请在您的问题中包含 div html。
比较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"/>
我遇到了一个问题,ng-if="expression value is 0"
它无法正常工作。
这里是 link 给 plunker 的消息:http://plnkr.co/edit/dILXSIHHzvuv1nhbxdga?p=info
在更改 select 时,我使用模型值来显示特定的文本框。
<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>
和输入字段
<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"/>
如果 location.locationSchedule.scheduleType == 1
它显示特定的文本框,但当它为 0 时不显示
那是因为父 ngIf
替换
ng-if="location.locationSchedule.scheduleType != ''"
和
ng-if="location.locationSchedule.scheduleType !== ''"
或者更合适的(cf Implied string comparison, 0='', but 1='1')
改变
ng-if="location.locationSchedule.scheduleType != ''"
至
ng-if="location.locationSchedule.scheduleType !== ''"
因为0 == ''
,两者都是假的,但它们的类型不一样。
不知道为什么每个人都在比较
现在我明白为什么了,为了清楚起见,请在您的问题中包含 div html。scheduleType !== ''
比较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"/>