簡體   English   中英

Angular:模板中的類型檢查無法正常工作

[英]Angular: Type checking in templates not working correctly

一段時間以來,類型檢查在 VSCode 中的 Angular 模板中不起作用。

我有以下代碼:

<div *ngIf="activeProperty" class="mx-3">
    <!-- some other code -->
    <input
        required
        id="propertyName"
        type="text"
        class="form-control"
        name="propertyName"
        [ngModel]="activeProperty.uiProperty.label"
        (ngModelChange)="updateLabelAndKeyInSpec($event)"
    />
</div>

現在在這一行:

[ngModel]="activeProperty.uiProperty.label"

activeProperty加下划線,告訴我The expression might be null ,因為我正在檢查由這顯然是錯誤的*ngIf以上。

請在此處查看我的ng --version

    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/


Angular CLI: 8.3.6
Node: 12.7.0
OS: win32 x64
Angular: 8.2.8
... animations, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.803.6
@angular-devkit/build-angular     0.803.6
@angular-devkit/build-optimizer   0.803.6
@angular-devkit/build-webpack     0.803.6
@angular-devkit/core              8.3.6
@angular-devkit/schematics        8.3.6
@angular/cdk                      8.2.2
@angular/cli                      8.3.6
@angular/http                     7.2.15
@ngtools/webpack                  8.3.6
@schematics/angular               8.3.6
@schematics/update                0.803.6
rxjs                              6.5.3
typescript                        3.5.3
webpack                           4.39.2

難道我做錯了什么?

我無法詳細告訴你為什么它*ngIf下划線......也許是因為它不知道*ngIf左右......但這應該可以解決問題:

[ngModel]="activeProperty?.uiProperty?.label"

你可以使用 activeProperty!.uiProperty!.label

[(ngModel)]="activeProperty.uiProperty.label"

你能試試這個嗎?

暫無
暫無

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

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