0
votes

As when we use reactive form we have to import FormBuilder, FormGroup, Validators classes of forms. Why we do not need to inject Validators to use it like FormBuilder.

    constructor(private formbuilder: FormBuilder) {
        this.loginForm = formbuilder.group({
          'email': [null, Validators.required],
          'password': [null, Validators.required]
        });
      }
1
Validators is a static class. There's no need to inject a static class because it's essentially in the global scope. - Brad
Dependency injection: is programing paradim. Its help developer to write better code and testable code. It is design pattern, where all composition/creation logic will stay in a service. It is not solution of all. Validator is just class which having methods and properties which can reuse. It doesnt have any creation logic inside. - xdeepakv

1 Answers

0
votes

They aren't the same thing. Injectable services are essentially singletons, you use them that way so they maintain state. Validators don't need that. They check whenever the field is updated and don't need to maintain a state.