簡體   English   中英

ng-if為0時的角度問題

[英]Angular issue when ng-if is 0

ng-if="expression value is 0"時,我遇到了問題,因為它被認為是行不通的。

這是到pl車的鏈接: http ://plnkr.co/edit/dILXSIHHzvuv1nhbxdga?p=info

更改選擇后,我將使用模型值來顯示特定的文本框。

<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 !== ''"

或更合適的方法(參見隱式字符串比較,0 ='',但1 ='1'

更改

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

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

beacause 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"/>

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM