Comments (4)
This should work fine with input.required
:
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.
from angular.
Angular can't bind an input while constructing the class. Anything that happens before the binding will throw NG950.
Your currentBar
getter is run during class contruction, same as your bar()
invokation in the constructor.
effect()
is scheduled to run only after init and the computed
is executed during CD (so after init).
from angular.
tl;dr: Shouldn't the docs say the same as the NG0950 error page is saying - that the input
signals will be guaranteed available in the ngOnInit
lifecycle and not before?
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:
This can happen when a required input is accessed too early in your directive or component. This is commonly happening when the input is read as part of class construction.
Inputs are guaranteed to be available in the ngOnInit lifecycle hook and afterwards.
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 always
is too strong in here imho)
Required inputs always have a value of the given input type.
Or am I am on the wrong track here?
from angular.
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.
from angular.
Related Issues (20)
- Differences in change detection between components using @for vs *ngFor HOT 1
- NgZoneSchedulingMode not exported HOT 1
- Page Not Found HOT 1
- Functions are missing from localForage after updating Angular to latest version HOT 2
- "updateOn: 'blur'" does not work in formBuilder/formControl HOT 11
- App-shell docs are out-of-date
- https://angular.dev/guide/security not found HOT 2
- Destroy component itself HOT 5
- Image lazy loading doesn't work in SSR when using ngTemplateOutlet and disabled hydration
- @angular/compiler `parseTemplate` fn does not properly parse `HTML` string element text content containing `@` HOT 2
- Angular 17 SSR: Slow server response time when using wildcard route HOT 5
- ERROR LOADING PAGES AFTER USE router.navigate
- SSR fails when root AppComponent in index.html is self-closing HOT 1
- foo = signal(''): if string is empty, !!foo () returns false HOT 1
- Form input mask provided by the framework HOT 6
- [Feature] Introduce Type-safe Token Providing HOT 6
- docs: `inject()` is missing all overloads
- Add distinction between model() and model.required() on type level HOT 1
- Standalone Component Exports HOT 1
- Set ViewEncapsulation per module HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from angular.