0

I'm sorry if it's a duplicate of someone question. However I couldn't find solution for my problem.

Angular 7. I need to input my variable as @Input into the ng-container via ngComponentOutlet. As says documents, there is an option to make it via Injector. And it's fine if it's just a string or number. But I have a little bit complicate thing: what if I want to have in injecting class more complicated logic: for example bind an Observable<SomeModel> that I want to get from ngrx store? Or subscribe on it?

Here I got folowing issue: Cannot read property 'select' of undefined.

@Injectable()
export class Greeter {
    suffix$;
    constructor(private store: Store<fromStore.State>) {
        this.suffix$ = this.store.select(fromStore.getSuffix);
    }
}

@Component({
    selector: 'complete-component',
    template: `Complete: {{ greeter.suffix$ | async }}`
})
export class CompleteComponent {
    constructor(public greeter: Greeter) {
        this.greeter.suffix$.subscribe(data => console.log('data', data));
        // not working
    }
}

@Component({
    selector: 'ng-component-outlet-complete-example',
    template: `
        <ng-container *ngComponentOutlet="CompleteComponent; 
                                        injector: myInjector;"
    })
export class NgTemplateOutletCompleteExample {
    CompleteComponent = CompleteComponent;
    myInjector: Injector;

    constructor(injector: Injector) {
        this.myInjector =
            Injector.create({providers: [{provide: Greeter, deps: []}], parent: injector});
    }
}

What am I doing wrong?

hofshteyn
  • 1,272
  • 4
  • 16
  • 33
  • As far as I can see, the `Greeter` class injects the `Store`. When you create `myInjector`, the `deps` array does not have any values. – Andrei Gătej Nov 07 '19 at 15:10
  • should I put some in `deps`? If so then what? I've read manual... but couldn't undertand what does it for - `deps` – hofshteyn Nov 07 '19 at 15:27
  • The ‘deps’ array should contain whatever you are injecting into the ‘Greeter’ class. I think that’s why you’re getting that error. It’s because the store is undefined in the Greeter class – Andrei Gătej Nov 07 '19 at 15:34
  • @AndreiGătej I created one more question according this theme. Could you check it, maybe you'll have some recommendation. https://stackoverflow.com/questions/58931410/angular-7-injection-with-dependencies-for-ngcomponentoutlet – hofshteyn Nov 19 '19 at 10:36

0 Answers0