簡體   English   中英

NgModel在角度測試中未更新類型無線電的輸入

[英]NgModel not Updating for Input of Type Radio in Angular Testing

我在Angular 2應用程序中input以下內容:

<input 
    id="radio-{{geo.id}}"
    name="radio-geos"
    type="radio" 
    [value]="geo.value"
    [(ngModel)]="selectedGeo">

selectedGeo綁定到public selectedGeo: string;

我為輸入元素編寫了以下內容:

it('should select the US Home Region', (done) => {
        // Arrange
        const component = TestBed.createComponent(regionComponent);

        // Act
        component.detectChanges();
        const usGeoRadioButton = component.debugElement.query(By.css('#radio-0'));                

        usGeoRadioButton.triggerEventHandler('click', null);
        component.detectChanges();
        const selectedGeo = component.componentInstance.selectedGeo;

        // Assert
        component.whenStable().then(() => {
            expect(selectedGeo).toBe('US');
            done();
        });
    });

在Karma中運行測試后,我注意到未單擊單選按鈕,並且未更新selectedGeo 我的測試失敗,因為selectedGeo返回為undefined。

我不確定為什么會這樣,並且我已經對dispatchEvent以及[(ngModel)]的更新方式進行了一些研究,但我無法弄清楚。

有什么建議么?

您的輸入標簽不應采用這種格式(在value和ng-model周圍沒有方括號):

<input type="radio" ng-model="string" value="string" [name="string"]...>

Doc鏈接: https : //docs.angularjs.org/api/ng/input/input%5Bradio%5D

暫無
暫無

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

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