Coder Social home page Coder Social logo

Comments (4)

alxhub avatar alxhub commented on April 28, 2024 2

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.

JeanMeche avatar JeanMeche commented on April 28, 2024 1

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.

fen89 avatar fen89 commented on April 28, 2024

@alxhub

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.

angular-automatic-lock-bot avatar angular-automatic-lock-bot commented on April 28, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.