Using angular custom decorator to initialize the property with value

40 views Asked by At

I've created a custom angular decorator

    export function InitializeForm(formGroup: FormGroup, formOptions: any[]) {
    return function (constructor: Function) {
        console.log(constructor);
        console.log(formGroup);
        console.log(formOptions);
        const originalNgOnInit = constructor.prototype.ngOnInit;

        constructor.prototype.ngOnInit = function () {
            console.log('we\'re here');
            for (let prop in this) {
                console.log(this[prop]);
            }

            originalNgOnInit.apply(this);
        };
    };
}

This decorator has a goal of initializing the FormGroup object with whatever is going to be passed to the decorator as a parameter.

@Component({
  selector: 'app-some-form-component',
  templateUrl: './some-form-component.component.html',
  styleUrls: ['./some-form-component.component.css']
})
@InitializeForm(FormsDefinitions.exampleStoreForm, [FormsDefinitions.statesDropdownList])
export class SomeFormComponentComponent implements OnInit {
  SomeFormGroup!: FormGroup;
  constructor() { }

  ngOnInit() {
  }

}

I tried to access the property list in the decorator the same way I've seen that on one of the videos explaining custom decorators. Unfortunately, this[prop] only displays one line in the console. Specifically a numeric value 1. Is there any way to access the property list in my case and assign the passed form group to that property?

0

There are 0 answers