question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NgModel value is always null and change event never triggered. But value has changed.

See original GitHub issue

Bug Report

Affected Package

@angular/forms

Description

The value of NgModel is always null and no change events are fired. But the NgModel itself works. In browser console I see some private keys with the lastValue which is correct and a proof for me that this is the correct NgModel link to the input element. (__ngContext__ see console).

Maybe my expectation is completely wrong. Either this is a bug or a lack of docs, etc. But we can handle an unexpected behavior as a bug. This results in one questions.

How to access (read / write) the NgModel value of the host element (e.g. input) from a directive?

There are some threads on Stackoverflow, etc. But nothing helps. … I’ve tried everything I can’t get this to life. Please teach me if I’m wrong. But this is a bug or a lack in my opinion.

Btw. the <input> element is just an example. I don’t want to read the native value from this element. For example: Some frameworks wraps the native input element in a not accessible scope. So I want to get the current NgModel value. This has nothing to do with <input>. It’s just about the NgModel.

Minimal Reproduction

Stackblitz Demo

import { Directive, HostListener } from '@angular/core';
import { NgModel } from '@angular/forms';

@Directive({ selector: 'input[appSome]', providers: [NgModel] })
export class SomeDirective {
  constructor(private ngModel: NgModel) {}

  // Or other events....
  @HostListener('ngModelChange')
  inputChanged() {
    console.log('Changed:', this.ngModel.value);
    // Result: Changed: null (always)
  }
}

Your Environment

Angular Version:

Your global Angular CLI version (12.0.2) is greater than your local version (11.0.2). 
The local Angular CLI version is used.

Angular CLI: 11.0.2
Node: 14.15.1
OS: win32 x64

Angular: 11.0.2
... 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.1100.2
@angular-devkit/build-angular   0.1100.2
@angular-devkit/core            11.0.2
@angular-devkit/schematics      11.0.2
@schematics/angular             11.0.2
@schematics/update              0.1100.2
rxjs                            6.6.3
typescript                      4.0.5

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
petebacondarwincommented, Jun 9, 2021

Yes I was just about to say the same as @jnizet: https://stackblitz.com/edit/angular-ivy-ngmodel-directive-issue-rf2pfy?file=src%2Fapp%2Fsome.directive.ts

Adding the NgModel to the providers actually creates a new NgModel “service” on the injector for this directive, which shadows the ngModel directive on the host element.

Closing because this works as expected.

1reaction
jnizetcommented, Jun 9, 2021

I tested that without the providers: [NgModel] that you have in your stackblitz. If you remove it, everything works fine, including using ngModel.value .

Read more comments on GitHub >

github_iconTop Results From Across the Web

ngModelChange value undefined when passed to function
The main issue seems to be with the collectFieldValue function because, without it, when I console log the value, the value never becomes ......
Read more >
How to fix NgModelChange Previous Value issue in Angular
On the first hand, it looks quite normal yeah?, maybe Angular works this way that ngModelChange is fired before the value is sync...
Read more >
"click event triggers before ng-model changes the value." #3406
I have problems with this too. One workaround is to use [(ngModel)] on select instead of (change) and declade get/set methods in the...
Read more >
Manually Triggering ngOnChanges() Inside An NgModel ...
Ben Nadel looks at how to trigger the ngOnChanges() life-cycle event method manually from within an ngModel value accessor bridge in Angular ...
Read more >
NgModel - Angular
It accepts a domain model as an optional Input . If you have a one-way binding to ngModel with [] syntax, changing the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found