0

我尝试使用动态 HTML 制作它,但我无法在动态 HTML 中调用点击事件。

这是我自己尝试的

my .ts file
htmlgrid:any;
jsonData:any;
 ngOnInit(){

this.htmlgrid= this.parse(this.jsonData)

}




createRow (r) {
    return '<div  style="background-color : ' + r.color + '" class="row">' +
      (r.text ? r.text : '') + this.parse(r) + '</div>';
  }
  createColumn (c) {
    return '<div style="background - color: red;" class="col-md-' + 6 + ' test">' +
      (c.text ? c.text : '') + this.parse(c) + '<img  click="hell();"  src = "../../../../assets/img/collection.jpg" style = "height: 100px; width:auto;" />' + '</div>';
  }
  parse (s) {
    let S = '';
    if (s.rows) {
      for (let i in s.rows) {
        console.log(s.rows[ i ], 'i of data');
        S += this.createRow(s.rows[ i ]);
      }
    }
    if (s.columns) {
      for (let i in s.columns) {
        S += this.createColumn(s.columns[ i ]);
      }
    }
    console.log(S, 'value of s');
    return S;
  }

我的.html 文件

<div class="one" [innerHtml]="htmlToAdd"></div>

这种类型的 JSON 用于制作行和列,我们的 JSON 中也有标识符和行列检查。请帮助我卡在这里太糟糕了,我需要在下面的json的基础上制作行和列的网格

this.jsonData={
"rows":[ 
         { 
            "columns":[ 
               { 
                  "identifier":"c1",
                  "hasRows":false,
                   "cashBack":{ 
                     "text":""
                  },
                  "title":{ 
                     "text":""
                  },
                  "images":{ 
                     "leafBanner":{ 
                        "url":"",
                        "bannerName":"",
                        "bannerType":"",
                        "bannerTarget":""
                     },
                     "listPageBanner":{ 
                        "image":"",
                        "2X":{ 
                           "height":"200px",
                           "width":"400px"
                        },
                        "3X":{ 
                           "height":"300px",
                           "width":"600px"
                        }
                     }
                  },
                  "height":"50",
                  "width":"50"
               },
               { 
                  "identifier":"c2",
                  "hasRows":false,
                  "cashBack":{ 
                     "text":""
                  },
                  "title":{ 
                     "text":""
                  },
                  "images":{ 
                     "leafBanner":{ 
                        "url":"",
                        "bannerName":"",
                        "bannerType":"",
                        "bannerTarget":""
                     },
                     "listPageBanner":{ 
                        "image":"",
                        "2X":{ 
                           "height":"200px",
                           "width":"400px"
                        },
                        "3X":{ 
                           "height":"300px",
                           "width":"600px"
                        }
                     }
                  },
                  "height":"50",
                  "width":"50"
               }
            ]
         },
         { 
            "columns":[ 
               { 
                  "identifier":"c3",
                  "hasRows":false,
                   "cashBack":{ 
                     "text":""
                  },
                  "title":{ 
                     "text":""
                  },
                  "images":{ 
                     "leafBanner":{ 
                        "url":"",
                        "bannerName":"",
                        "bannerType":"",
                        "bannerTarget":""
                     },
                     "listPageBanner":{ 
                        "image":"",
                        "2X":{ 
                           "height":"200px",
                           "width":"400px"
                        },
                        "3X":{ 
                           "height":"300px",
                           "width":"600px"
                        }
                     }
                  },
                  "height":"33",
                  "width":"33"
               },
               { 
                  "identifier":"c4",
                  "hasRows":false,
                   "cashBack":{ 
                     "text":""
                  },
                  "title":{ 
                     "text":""
                  },
                  "images":{ 
                     "leafBanner":{ 
                        "url":"",
                        "bannerName":"",
                        "bannerType":"",
                        "bannerTarget":""
                     },
                     "listPageBanner":{ 
                        "image":"",
                        "2X":{ 
                           "height":"200px",
                           "width":"400px"
                        },
                        "3X":{ 
                           "height":"300px",
                           "width":"600px"
                        }
                     }
                  },
                  "height":"33",
                  "width":"33"
               },
               { 
                  "identifier":"c5",
                  "hasRows":false,
                   "cashBack":{ 
                     "text":""
                  },
                  "title":{ 
                     "text":""
                  },
                  "images":{ 
                     "leafBanner":{ 
                        "url":"",
                        "bannerName":"",
                        "bannerType":"",
                        "bannerTarget":""
                     },
                     "listPageBanner":{ 
                        "image":"",
                        "2X":{ 
                           "height":"200px",
                           "width":"400px"
                        },
                        "3X":{ 
                           "height":"300px",
                           "width":"600px"
                        }
                     }
                  },
                  "height":"33",
                  "width":"33"
               }
            ]
         }
      ]
}
4

1 回答 1

0

您可以定义一个将生成 html 的组件,并且可以递归调用它

@Component({
  selector: "grid",
  template: `
    <ng-container [ngSwitch]="(data | keyvalue)[0].key">
      <ng-container *ngSwitchCase="'rows'">
        <div class="row" *ngFor="let row of data.rows">
          <grid [data]="row"></grid>
        </div>
      </ng-container>
      <ng-container *ngSwitchCase="'columns'">
        <div class="col" *ngFor="let col of data.columns">
          <grid [data]="col"></grid>
        </div>
      </ng-container>
      <ng-container *ngSwitchDefault>
        <grid [data]="data.rows" *ngIf="data.hasRows; else cell"></grid>
        <ng-template #cell>
          <div class="cell">{{ data | json }}</div>
        </ng-template>
      </ng-container>
    </ng-container>
  `,
  styles: [
    ".row{background-color:red;padding: 5px;}",
    ".col{background-color:green; padding:5px;}",
    ".cell{background-color:cyan;padding:5px;}"
  ]
})
export class GridComponent {
  @Input()
  data: any;
}

您可以像这样从您的应用程序/显示组件调用此网格组件

<grid [data]="jsonData"></grid>

这是一个良好的开端,您可以修改默认开关盒以满足您的需求我刚刚hasRows在您的 json 中找到了一个属性,如果这是真的,它将递归地再次调用网格组件。

希望对您有所帮助,Stackblitz 供您参考:https ://stackblitz.com/edit/angular-6cqbsg

于 2019-10-12T05:04:24.140 回答