1

財產「符號」我有具有FormBuilder形式的頁面組件:NativeScript /角 - 類型錯誤:無法讀取的不確定

public adForm: FormGroup = this.fb.group({ 
    questions: this.fb.array([]) 
}); 

我通過questions下降到<questions>組件:

<questions [questions]="adForm.get('questions')"></questions> 

其中有一個@Input() questions: FormArray;,並使用這個模板:

<StackLayout orientation="vertical"> 
    <!-- If I comment out listview then it doesn't throw the error --> 
    <ListView [items]="questions"> 
    <ng-template let-question="item"> 
     <Label [text]="question.model"></Label> 
    </ng-template> 
    </ListView> 
    <Button class="btn btn-md btn-primary" text="Add question" (tap)="addQuestion()"></Button> 
</StackLayout> 

我面臨的問題是,ListView位引發此錯誤:

TypeError: Cannot read property 'Symbol' of undefined 

如果我評論說,部分出來那麼它不會引發錯誤。

我知道它與questions表單數組有關,但我一直無法弄清楚什麼。它是這樣定義的,所以它不應該成爲undefined而不是空陣列的問題。

請注意,此錯誤直接在組件init上引發。我在ngOnInit中記錄了questions,它不是未定義的。

我在做什麼錯?

回答

1

我完全忘記了,如果我傳遞一個表單數組,我實際上並沒有獲得數組的原始值。所以在ListView中,我必須執行questions.valuequestions.controls以循環它們。

我希望這可以幫助別人在未來。

+1

的感謝!在閱讀完之後我檢查,看看是否我其實是遍歷數組,但數組實際上是隱藏的更深一層 –

+0

@BartvandenBurg很高興它幫助你:) – Chrillewoodz

1

只是爲了給樣板添加一些東西,我遇到了這個問題以及更簡單的列表,儘管我使用的組件是telerik的RadListView,它仍然基於nativescript的核心ListView。

相關HTML是:

<GridLayout tkExampleTitle tkToggleNavButton> 
     <RadListView class="list-group" ng-if="!isLoading" [items]="getAsyncList"> 
      <ng-template tkListItemTemplate let-item="item"> 
       <StackLayout class="list-group-item" orientation="vertical"> 
        <Label class="list-group-item-heading" [text]="item.name"></Label> 
       </StackLayout> 
      </ng-template> 
     </RadListView> 
    </GridLayout> 

我遇到這個錯誤藏漢,雖然從控制檯它不是那麼容易發現問題:

JS: ERROR TypeError: Cannot read property 'Symbol' of undefined 
JS: ERROR CONTEXT [object Object] 

所以,運行像一個瘋狂的後花了30分鐘左右,我發現問題比我期望的要容易得多。

items PARAMS應該是一個getter的結果,但我沒有,相反,在我的組件相同的名稱定義一個函數:

public getAsyncList() { 
    return this.ListViewCollection; 
} 

所以它可能是引用功能的列表,而不是一個吸氣劑,因此我不得不將其更改爲:

public get getAsyncList() { 
    return this.ListViewCollection; 
} 

(注意get)。

有點難以追查到這個問題,但嘿,無論如何這是我的錯:P。

相關問題