在Angular2中使用ComponentResolver和ngFor创建动态anchorName / Components

前端之家收集整理的这篇文章主要介绍了在Angular2中使用ComponentResolver和ngFor创建动态anchorName / Components前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要在ngFor循环中创建唯一的锚点名称/组件,以将其与ComponentResolver.resolveComponent一起使用.
  1. <div>
  2. <table>
  3. <tr *ng-for="#vIndex of vArr">
  4. <td *ng-for="#hIndex of hArr">
  5. <div #uniqueanchorname{{vIndex}}_{{hIndex}}></div>
  6. </td>
  7. </tr>
  8. </table>
  9. </div>

生成的HTML应该看起来像这样:

  1. <div>
  2. <table>
  3. <tr>
  4. <td>
  5. <div #uniqueanchorname0_0></div>
  6. </td>
  7. <td>
  8. <div #uniqueanchorname0_1></div>
  9. </td>
  10. </tr>
  11. <tr>
  12. <td>
  13. <div #uniqueanchorname1_0></div>
  14. </td>
  15. <td>
  16. <div #uniqueanchorname1_1></div>
  17. </td>
  18. <td>
  19. <div #uniqueanchorname1_2></div>
  20. </td>
  21. </tr>
  22. </table>
  23. </div>

有了它,我可以使用DynamicComponentLoader,如:

  1. loader.loadIntoLocation(responseDependentComponent,elementRef,'uniqueAnchorName1_2');

生成的HTML不会被替换,看起来像:

  1. <div>
  2. <table>
  3. <tr>
  4. <td>
  5. <div #uniqueanchorname{{vIndex}}_{{hIndex}}></div>
  6. </td>
  7. <td>
  8. <div #uniqueanchorname{{vIndex}}_{{hIndex}}></div>
  9. </td>
  10. </tr>
  11. <tr>
  12. <td>
  13. <div #uniqueanchorname{{vIndex}}_{{hIndex}}></div>
  14. </td>
  15. <td>
  16. <div #uniqueanchorname{{vIndex}}_{{hIndex}}></div>
  17. </td>
  18. <td>
  19. <div #uniqueanchorname{{vIndex}}_{{hIndex}}></div>
  20. </td>
  21. </tr>
  22. </table>
  23. </div>

如果无法创建唯一的锚名称.还有其他方法可以将组件加载到特定位置吗?

对不起,这是一个误解.
  1. import {
  2. Directive,Component,View,CORE_DIRECTIVES,ElementRef,DynamicComponentLoader,Input,QueryList,ViewChildren
  3. } from 'angular2/angular2'
  4.  
  5. @Component({
  6. selector: 'my-cmp'
  7. })
  8. @View({
  9. template: 'my component'
  10. })
  11. class MyCmp {}
  12.  
  13. @Directive({
  14. selector: '[location]'
  15. })
  16. class Location {
  17. @Input() h: number;
  18. @Input() v: number;
  19. constructor(public elementRef: ElementRef) {
  20. }
  21. }
  22.  
  23. @Component({
  24. selector: 'my-table'
  25. })
  26. @View({
  27. template: `
  28. <table border>
  29. <tr *ng-for="#v of vArr">
  30. <td *ng-for="#h of hArr">
  31. <div location v="{{v}}" h="{{h}}">{{v}}-{{h}}</div>
  32. </td>
  33. </tr>
  34. </table>
  35. h:<input #hi value="1"><br>
  36. v:<input #vi value="2"><br>
  37. <button (click)="load(hi.value,vi.value)">load</button>
  38. `,directives: [CORE_DIRECTIVES,Location]
  39. })
  40. class MyTable {
  41. vArr = [1,2,3];
  42. hArr = [1,3];
  43. @ViewChildren(Location) locations: QueryList;
  44. constructor(
  45. private loader: DynamicComponentLoader,) {
  46. }
  47.  
  48. load(h,v) {
  49. let elementRef = null;
  50. for(let i = 0; i < this.locations._results.length; i++) {
  51. if(this.locations._results[i].h == h && this.locations._results[i].v ==v) {
  52. elementRef = this.locations._results[i].elementRef;
  53. }
  54. }
  55.  
  56. if(elementRef) {
  57. this.loader.loadNextToLocation(MyCmp,elementRef);
  58. }
  59. }
  60. }
  61.  
  62. @Component({
  63. selector: 'my-app'
  64. })
  65. @View({
  66. template: `<my-table></my-table>`,directives: [MyTable]
  67. })
  68. export class App {}

http://plnkr.co/edit/dqfPCW3MBa9hM23EW3cS?p=preview这就是你需要的吗?

猜你在找的Angularjs相关文章