简体   繁体   English

Angular:模板中的类型检查无法正常工作

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

Since some time the type checking does not work in the templates of Angular in VSCode.一段时间以来,类型检查在 VSCode 中的 Angular 模板中不起作用。

I have the following code:我有以下代码:

<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>

Now at this line:现在在这一行:

[ngModel]="activeProperty.uiProperty.label"

activeProperty is underlined, telling me that The expression might be null which is clearly wrong because I'm checking that by the *ngIf above. activeProperty加下划线,告诉我The expression might be null ,因为我正在检查由这显然是错误的*ngIf以上。

Please see my ng --version here:请在此处查看我的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

Am I doing something wrong?难道我做错了什么?

我无法详细告诉你为什么它*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