Skip to content

Two-way binding not work with non-null assertion operator #36551

Closed

Description

@hsuanxyz

🐞 bug report

Affected Package

@angular/compiler-cli or @angular/compiler ?

Is this a regression?

No

Description

I enabled strictTemplates in my project and follow this guide to fix type errors.

But when I add a non-null assertion operator to the two-way binding value, the compilation result does not meet expectations.

🔬 Minimal Reproduction

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  template: `
    <input type="checkbox" [(ngModel)]="obj.value!">
    <!--                                ^^^^^^^^^ -->
  `,
  styleUrls: ['./app.component.less']
})
export class AppComponent {
  obj: {
    value?: boolean
  } = {}
}

🔥 Exception or Error

Expected

function AppComponent_Template(rf, ctx) {
    if (rf & 1) {
        i0.ɵɵelementStart(0, "input", 0);
        i0.ɵɵlistener("ngModelChange", function AppComponent_Template_input_ngModelChange_0_listener($event) {
            return ctx.obj.value = $event;
            //                     ^^^^^^^
        });
        i0.ɵɵelementEnd();
    }
    if (rf & 2) {
        i0.ɵɵproperty("ngModel", ctx.obj.value);
    }
}

Actually

function AppComponent_Template(rf, ctx) {
    if (rf & 1) {
        i0.ɵɵelementStart(0, "input", 0);
        i0.ɵɵlistener("ngModelChange", function AppComponent_Template_input_ngModelChange_0_listener($event) {
            return ctx.obj.value != $event;
            //                   ^^^^^^^^^
        });
        i0.ɵɵelementEnd();
    }
    if (rf & 2) {
        i0.ɵɵproperty("ngModel", ctx.obj.value);
    }
}

🌍 Your Environment

Angular Version:



Angular CLI: 9.1.1
Node: 12.16.1
OS: darwin x64

Angular: 9.1.1
... animations, cli, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router
Ivy Workspace: Yes

Package                            Version
------------------------------------------------------------
@angular-devkit/architect          0.901.1
@angular-devkit/build-angular      0.901.1
@angular-devkit/build-ng-packagr   0.901.1
@angular-devkit/build-optimizer    0.901.1
@angular-devkit/build-webpack      0.901.1
@angular-devkit/core               9.1.1
@angular-devkit/schematics         9.1.1
@angular/cdk                       9.2.0
@ngtools/webpack                   9.1.1
@schematics/angular                9.1.1
@schematics/update                 0.901.1
ng-packagr                         9.1.0
rxjs                               6.5.5
typescript                         3.8.3
webpack                            4.42.0

Anything else relevant?

Current solution

<input type="checkbox" [(ngModel)]="!!obj.value">
function AppComponent_Template(rf, ctx) {
    if (rf & 1) {
        i0.ɵɵelementStart(0, "input", 0);
        i0.ɵɵlistener("ngModelChange", function AppComponent_Template_input_ngModelChange_0_listener($event) {
            return !!(ctx.obj.value = $event);
        });
        i0.ɵɵelementEnd();
    }
    if (rf & 2) {
        i0.ɵɵproperty("ngModel", !!ctx.obj.value);
    }
}

or

<input type="checkbox" [(ngModel)]="$any(obj).value">

or

<input type="checkbox" [ngModel]="obj.value!" (ngModelChange)="obj.value = $event">

Activity

  1. added
    area: compiler
    on Apr 12, 2020
  2. added this to the Backlog milestone on Apr 12, 2020
  3. JoostK commented on Apr 12, 2020

    @JoostK
    Member

    Tracking as FW-2071

  4. added
    P3
    and removed on Oct 1, 2020
  5. th0r commented on Aug 25, 2021

    @th0r

    Are there any plans to fix this issue guys? Just faced it as well.

  6. added a commit that references this issue on Feb 4, 2022
    9751bcb
  7. added a commit that references this issue on Feb 7, 2022
    37af6ab
  8. angular-automatic-lock-bot commented on Mar 10, 2022

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  9. locked and limited conversation to collaborators on Mar 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3area: compilercompiler: parserfreq1: lowstate: confirmedstate: has PRtype: bug/fix

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions