Mein CoreComponent
meine NotificationsService
erzählt eine neue Meldung zu zeigen und übergibt eine HTML-Zeichenfolge in der content
Immobilie finden:kann nicht variabel nach innen * ngFor in Angular 2
export class CoreComponent {
constructor(private _notificationsService: NotificationsService) {
this._notificationsService.show({content: '<h1 (click)="close()">hej hej</h1>'});
}
}
Dann ist mein NotificationsComponent
ins Spiel kommt, nachdem die NotificationsService
haben die Mitteilung an die Komponente weitergeleitet, muss diese Komponente eine gefälschte Komponente machen, so dass alle Bindungen und auf der hTML-in content
gesetzt Richtlinien arbeiten:
export class NotificationsComponent {
public notifications: string[] = [];
constructor(private _notificationsService: NotificationsService, dcl: DynamicComponentLoader, elementRef: ElementRef, injector: Injector) {
_notificationsService.newNotification$.subscribe(
notification => {
this.notifications.push(notification);
if (notification.content) {
dcl.loadIntoLocation(toComponent(notification.content, []), elementRef, 'content');
function toComponent(template, directives = []) {
@Component({
selector: 'custom-content',
template: template,
directives: directives
})
class FakeComponent {}
return FakeComponent;
}
}
}
)
}
}
Dann macht er eine Benachrichtigung mit einer *ngFor
Schleife:
<div>
<ul>
<li *ngFor="#notification of notifications">
<div #content></div>
</li>
</ul>
</div>
Das Problem ist aber, dass es nicht die #content
Variable aus irgendeinem Grunde nicht finden, es wirft nur ein Uncaught Could not find variable content
. Meine Vermutung ist, weil es in einer Schleife ist, aber ich weiß wirklich nicht warum, wenn das der Fall ist.
Wie kann ich diese gefälschte Komponente in die Benachrichtigungsvorlage rendern (vorzugsweise vor dem Push notification
in public notifications
, so dass die Komponente tatsächlich von Anfang an da ist, wenn eine neue Benachrichtigung angezeigt wird)?
EDIT: Ich fand heraus, dass ohne die *ngFor
mein Code funktioniert, aber es gibt etwas über die Schleife, die hier Probleme verursacht. Weiß jemand, warum das passiert?
Ich denke, das Problem ist, dass Sie mehrere Elemente mit dem '# content' Template-Variable zu erzeugen. –
@ GünterZöchbauer Aber wenn das der Fall ist, warum macht es nicht 1 Element und irgendwelche Elemente, die danach hinzugefügt werden, wirft einen Fehler auf? Macht keinen Sinn. – Chrillewoodz
Sie haben Recht, es sollte funktionieren. Siehe auch https://github.com/angular/angular/issues/6920 –