Создание шаблона HTML в JavaScript для цикла

0

Я создаю HTML-шаблон внутри цикла for, я хотел бы передать значение переменной в шаблон и создать динамические шаблоны.

Ниже мой цикл for, мне нужно значение colIndex, которое должно быть передано в теге Span HTML, упомянутом как "Need colIndex здесь",

for (var colIndex = 0; colIndex < colCount; colIndex++) {
  $scope.gridOptions.columnDefs.push({
    name: 'col-' + colIndex,
    aggregationType: uiGridConstants.aggregationTypes.sum,
    width: 85,
    treeAggregationType: uiGridTreeViewConstants.aggregation.SUM,
    cellTemplate: '<div class="ui-grid-cell-contents">'+
                  '<span ng-if="!(row.entity[\'$$\' + col.uid])">{{row.entity[\'col-'+'<<Need colIndex here>>'+'\'] CUSTOM_FILTERS}}</span>'+
                  '<span ng-if="row.entity[\'$$\' + col.uid]"> {{row.entity["$$" + col.uid].value CUSTOM_FILTERS}}</span>'+
                  '</div>'
  });
}

Пожалуйста, предложите.

  • 1
    Разве вы не можете просто передать его так же, как заполняете свойство name ?
  • 0
    Я думал о чем-то более сложном и получил базовую конкатенацию строк;) ...

1 ответ

0

Вы можете сделать это,

for (var colIndex = 0; colIndex < colCount; colIndex++) {
$scope.gridOptions.columnDefs.push({
name: 'col-' + colIndex,
aggregationType: uiGridConstants.aggregationTypes.sum,
width: 85,
treeAggregationType: uiGridTreeViewConstants.aggregation.SUM,
cellTemplate: '<div class="ui-grid-cell-contents">'+
              '<span ng-if="!(row.entity[\'$$\' + col.uid])">{{row.entity[\'col-'+colIndex+'\'] CUSTOM_FILTERS}}</span>'+
              '<span ng-if="row.entity[\'$$\' + col.uid]"> {{row.entity["$$" + col.uid].value CUSTOM_FILTERS}}</span>'+
              '</div>'
});
}
  • 0
    Да: D .. Я понял после публикации вопроса, это был очень глупый вопрос :) Спасибо, чувак ..

Ещё вопросы

Сообщество Overcoder
Наверх
Меню