angular / angular/components

question(drag-drop): createEmbeddedView for drag preview/placeholder templates

Offen
#20,047 3 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
area: cdk/drag-drop P3
Vorherrschende Sprache
TypeScript
Sterne
25k
Forks
6.8k
Ø Merge
1 T. 8 Std.
Gemergte PRs (30 T.)
91

Beschreibung

A few months back, I opened https://github.com/angular/components/issues/19060 that described a bug with `matchSize` possibly not working correctly due to the preview template being stamped out inside the drop list's view container...and afterward measuring the original drag item.

The solution I proposed in that thread was to just stamp out the preview template view outside of the DOM, so the drop list container children are never touched before being measured.

The fix for this issue was decided to be to measure the original drag item first, then stamp the preview template out in the existing manner in the drop list view container. This works, but the implementation still bugs me because the template is being stamped out in the view container, modifying the DOM, and then the drag preview element is immediately moved to bottom of the document body, modifying the DOM **again** (versus the other solution that only modifies the DOM once). Same idea for the placeholder template as well, how it is placed into the DOM and then immediately moved around (using `replaceChild`)...when it could just be put in the correct place from the start.

@crisbeto made the following comment:
>The idea behind inserting the view through the ViewContainerRef is to ensure that it's inside the same view/DI hierarchy as drag item. I agree that ideally we'd just create the element outside the DOM, but an alternate solution is to measure the element before the preview has been created.

but I am struggling to understand this comment (_"ensure that it's inside the same view/DI hierarchy as drag item"_). As far as I am aware, an injector cannot be specified when using `createEmbeddedView` and the injector used inside the preview template is simply the injector of the component in which the template is **defined**.

Issue about this (still open...) from 3 years ago:
https://github.com/angular/angular/issues/14935

So what would stamping a template out using
```ts
const viewRef = previewConfig!.viewContainer.createEmbeddedView(previewTemplate, previewConfig!.context);
```
or
```ts
const viewRef = previewTemplate.createEmbeddedView(previewConfig!.context);
```

have anything to do with dependency injection? I feel like the answer is "nothing" but I might be misunderstanding.

@crisbeto can you explain your comment in more detail as to why it is advantageous to use the view container and/or if something I've said above is not accurate?

Beitragsleitfaden

Beitragsleitfaden öffnen

Rechercherichtung

Beginne mit den hier beschriebenen Pfaden für die Erstellung der Drag-Vorschau und des Platzhalters und vergleiche dabei ViewContainerRef.createEmbeddedView mit TemplateRef.createEmbeddedView sowie der bestehenden Sequenz aus Messung und Reparenting. Als abgeschlossen gilt eine von einem Maintainer bestätigte Erklärung des DI/View-Hierarchie-Verhaltens und eine festgelegte Richtung; im Issue wird keine Datei und kein Test genannt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
angular, typescript
Bereich
frontend
Issue-Typ
Refactoring
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Muss geklärt werden
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.