create dynamic anchorName/Components with ComponentResolver and ngFor in Angular2

前端 未结 2 610
心在旅途
心在旅途 2020-12-06 12:37

I need to create unique Anchor Names/Components in a ngFor loop to use it with ComponentResolver.resolveComponent.

相关标签:
2条回答
  • 2020-12-06 13:00

    Sorry, there's been a misunderstanding.

    import {
      Directive, 
      Component, 
      View, 
      CORE_DIRECTIVES, 
      ElementRef, 
      DynamicComponentLoader,
      Input,
      QueryList,
      ViewChildren
    } from 'angular2/angular2'
    
    @Component({
      selector: 'my-cmp'
    })
    @View({
      template: 'my component'
    })
    class MyCmp {}
    
    @Directive({
      selector: '[location]'
    })
    class Location {
      @Input() h: number;
      @Input() v: number;
      constructor(public elementRef: ElementRef) {
      }
    }
    
    @Component({
      selector: 'my-table'
    })
    @View({
      template: `
      <table border>
        <tr *ng-for="#v of vArr">
          <td *ng-for="#h of hArr">
            <div location v="{{v}}" h="{{h}}">{{v}}-{{h}}</div>
          </td>
        </tr>
      </table>
      h:<input #hi value="1"><br>
      v:<input #vi value="2"><br>
      <button (click)="load(hi.value, vi.value)">load</button>
      `,
      directives: [CORE_DIRECTIVES, Location]
    })
    class MyTable {
      vArr = [1, 2, 3];
      hArr = [1, 2, 3];
      @ViewChildren(Location) locations: QueryList;
      constructor(
        private loader: DynamicComponentLoader,
        ) {
      }
    
      load(h, v) {
        let elementRef = null;
        for(let i = 0; i < this.locations._results.length; i++) {
           if(this.locations._results[i].h == h && this.locations._results[i].v ==v) {
             elementRef = this.locations._results[i].elementRef;
           }
        }
    
        if(elementRef) {
          this.loader.loadNextToLocation(MyCmp, elementRef);
        }
      }
    }
    
    @Component({
      selector: 'my-app'
    })
    @View({
      template: `<my-table></my-table>`,
      directives: [MyTable]
    })
    export class App {}
    

    http://plnkr.co/edit/dqfPCW3MBa9hM23EW3cS?p=preview Is that what you need?

    0 讨论(0)
  • 2020-12-06 13:18

    Solution for Angular2 rc.0

    HTML:

    <div style="display: block">
      <table>
        <tr *ngFor="let v of arr; let i = index">
          <td *ngFor="let h of arr[i]; let j = index">
            <div location v="i" h="j"></div>
          </td>
        </tr>
      </table>
    </div>
    

    Location directive:

    @Directive({
      selector: '[location]'
    })
    class Location {
      @Input() h: number;
      @Input() v: number;
    
      constructor(public viewContainerRef: ViewContainerRef) {
      }
    }
    

    Component

    @Component({
      selector: 'example-cmp',
      directives: [CORE_DIRECTIVES, Location]
    })
    export class ExampleComponent implements AfterViewInit {
      public arr: Array<Array<number>> = [];
      public componentRef: ComponentRef<any>;
    
      @ViewChildren(Location) private locations: QueryList<Location>;
    
      constructor(private resolver: ComponentResolver) {
      }
    
      ngAfterViewInit() {
        this.loadTag(0,0,SomeViewComponent);
      }
    
      loadTag(x:number, y:number, component) {
        let viewContainerRef: ViewContainerRef = null;
        let locs = this.locations.toArray();
        for (let i = 0; i < locs.length; i++) {
          if (+locs[i].h === x && +locs[i].v === y) {
            viewContainerRef = locs[i].viewContainerRef;
          }
        }
        if (viewContainerRef != null) {
          var injector = ReflectiveInjector.fromResolvedProviders(ReflectiveInjector.resolve([
            provide(NeededAttribute, { useValue: 42 })]),
            this.componentRef.injector);
    
          this.resolver.resolveComponent(component).then((factory: ComponentFactory<any>) => {
            var compRef: ComponentRef<any> = viewContainerRef.createComponent(factory, -1, injector);
          });
        }
      }
    }
    
    0 讨论(0)
提交回复
热议问题