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.

ngValue ignored in ng-content and ng-template

See original GitHub issue

I’m submitting a…


[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report  
[ ] Performance issue
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question
[ ] Other... Please describe:

This issue was previously reported at https://github.com/angular/angular/issues/22374, which was closed as a support request even though I don’t believe it was intended to be one.

Current behavior

ngValue is ignored if (1) the <option>s are provided via <ng-content> to a component (even if the component simply wraps a vanilla <select>) or (2) if the <option>s are in an <ng-template>.

Expected behavior

ngValue should work the same in these cases as it does when using a plain <select> with directly nested <option>s.

Minimal reproduction of the problem with instructions

Please see https://codesandbox.io/s/2z2n2v0pjp .

Here is a screenshot of the reproduction:

image

What is the motivation / use case for changing the behavior?

Many applications require a custom-styled <select> component that allow the caller of the component to provide their own <option>s.

Environment


Angular version: 6

Browser:
- [x] All

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:9
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
pauldrapercommented, Apr 29, 2019

ngValue relies on DI of SelectControlValueAccessor.

Both failing examples the repro have the templated options defined outside any injector with SelectControlValueAccessor.


There is one case that will work, however. If SelectWrappedComponent provides SelectControlValueAccessor and the options are defined in its content, then it works.

I.e. This gives you the ability to write a decorated/custom select component with content-projected options that work well with ngModel/ngValue.

https://codesandbox.io/s/jrjnlvxl5

import { Component, forwardRef } from "@angular/core";
import { SelectControlValueAccessor, NG_VALUE_ACCESSOR } from "@angular/forms";

/** For simplicity, hackishly extend SelectControlValueAccessor to reuse its implementation. */
@Component({
  selector: "select-wrapped",
  template: `
    <select
      [selectedIndex]="properties.selectedIndex"
      [value]="properties.value"
      [disabled]="properties.disabled"
      [(ngModel)]="value"
      (blur)="onTouched()"
      (change)="onChange($event.target.value)"
    >
      <ng-content></ng-content>
    </select>
  `,
  styles: [],
  providers: [
    {
      provide: NG_VALUE_ACCESSOR,
      multi: true,
      useExisting: forwardRef(() => SelectWrappedComponent)
    },
    {
      provide: SelectControlValueAccessor,
      useExisting: forwardRef(() => SelectWrappedComponent)
    }
  ]
})
export class SelectWrappedComponent extends SelectControlValueAccessor {
  readonly properties;

  constructor() {
    super(
      <any>{
        setProperty(_, property, value) {
          this.properties = this.properties || {};
          this.properties[property] = value;
        }
      },
      {}
    );
    this.properties = this.properties || {};
  }
}
<select-wrapped [(ngModel)]="bin">
	<option [ngValue]="42">forty two (this value should be the number 42)</option>
	<option [ngValue]="43">forty three (this value should be the number 43)</option>
	<option [ngValue]="{ test: true }">test (this value should be an object)</option>
</select-wrapped>
0reactions
dylhunncommented, Oct 3, 2022

We looked at this again today. This is generally related to the lack of a robust composability model in Forms. Solving that general issue would also solve this one. The work is on the horizon, after we’re through the upcoming focus on reactivity.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ngValue is ignored when used inside ng-content in Angular
I have created a custom control say my-select-input in Angular that wraps native select input. Template looks like this.
Read more >
Everything you need to know about ng-template, ng-content ...
Angular wraps the host element (to which the directive is applied) inside <ng-template> and consumes the <ng-template> in the finished DOM by ...
Read more >
ngRepeat - AngularJS: API
The ngRepeat directive instantiates a template once per item from a collection. Each template instance gets its own scope, where the given loop...
Read more >
Angular 10 - Avoid using ::ng-deep (ngdeep)
CSS Query Selectors within a View's component style, are being ignored by Angular anytime we attempt to change a "deep" style. With Angular, ......
Read more >
Difference between ng-template, ng-container and ng-content
As the name suggests the is a template element that Angular uses with structural directives ( *ngIf , *ngFor , [ngSwitch] and custom ......
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