New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide input
property to options
of createComponent
#55032
Comments
This should work fine with const ref = viewContainerRef.createComponent(FooComponent);
ref.setInput('bar', true); Change detection won't run immediately upon creating the component, so you have time to set the inputs. |
tl;dr: Shouldn't the docs say the same as the NG0950 error page is saying - that the Here is a small stackblitz of the case: https://stackblitz.com/edit/stackblitz-starters-znzjc1?file=src%2Ffoo.component.ts @Component({
selector: 'app-foo',
standalone: true,
template: `
<p>I am a {{ bar() }} foo! - and I am the opposite: {{ noBar() }}</p>
`,
})
export class FooComponent implements OnInit {
bar = input.required<boolean>();
// this is working as expected
noBar = computed(() => !this.bar());
get currentBar() {
try {
return this.bar();
} catch (err) {
console.error('instant access not working');
}
return undefined;
}
// ! throws NG0950: Input is required but no value is available yet.
barWithoutComputed = this.currentBar;
constructor() {
try {
// ! throws NG0950: Input is required but no value is available yet.
this.bar();
console.log('constructor works');
} catch (err) {
console.error('constructor is not working');
}
afterRender(() => {
// this is working
this.bar();
console.log('afterRender works');
});
}
ngOnInit() {
// this is working
this.bar();
console.log('ngOnInit works');
}
} After looking into this and the error-code NG0950 which says:
In the docs page for Signal Inputs it says the following which can be kind of misleading ( I get that the intention was another one, but the word
Or am I am on the wrong track here? |
Angular can't bind an input while constructing the class. Anything that happens before the binding will throw NG950.
|
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
Which @angular/* package(s) are relevant/related to the feature request?
core
Description
Modals are usually created using
createComponent
.If we open a component that has signal
input
s, we can assign a value usingsetInput
on theComponentRef
.But if we use
input.required
and access the signal directly in the component, the error "NG0950: Input is required but no value is available yet." is thrown.So
setInput
will only be resolved with the next change detection.Proposed solution
Is it possible to provide another property
input
on theoptions
ofcreateComponent
to pass the required inputs directly?createComponent
should extract required inputs from the component and then require the options.input.Example usage:
This code snipped could be used to extract the signals from the component.
Only the optional inputs should be listed with a
?
.Alternatives considered
A workaround is to wrap the signal with
computed
- but this is not a clean solution.The text was updated successfully, but these errors were encountered: