使用自定义指令使用ng-repeat显示2个不同的表

neelmeg

我有一个自定义指令(生成表),该指令在索引页面上重复两次。表的值通过$ scope.globalrows变量填充。即使globalrows包含2个数组值,它始终会打印第二个值。如何修改模板或指令以显示该表唯一的值并防止内容覆盖。

问题:这些表在globalarray中的第二个表内容中重复了两次。我看到第二个表正在覆盖第一个表。

index.html

<grid-print section="1"></grid-print>
<grid-print section="2"></grid-print>

模板:print.dir.html

<form class="form-horizontal clearfix">
    <table class="grid table table-bordered">
      <thead>
        <tr>
          <th ng-repeat="col in cols">{{col.title}}</th>
        </tr>
      </thead>
      <tbody>
          <tr ng-repeat="row in globalrows track by $index" index={{index}}>
            <td ng-repeat="col in cols">          
              <span>{{ row[col.field] }}</span>
            </td>  
          </tr>
      </tbody>
    </table>
    </form>

指示:

.directive("gridPrint", function($sessionStorage, dataservice) {
    return {
        restrict: 'E',
        templateUrl: "components/print/templates/print.dir.html",
        replace: true,
        controller: function($scope, $state, $attrs) {
            //array of the items that are to be displayed on the 2 tables  
            $scope.globalrows = dataservice.getCollectionData();          

        },
        link: function(scope, element, attributes){
           // console.log("in grid print"+attributes['section']);            
        }
    };
})

另一个生成行的指令cols:

.directive("grid", function() {
    return {
        restrict: 'E',
        templateUrl: "components/section/directives/section.shared.dir.html",
        replace: true,
        controller: function($scope) {
            $scope.$on('ready-to-render', function(e, rows, cols) {
               // console.log(rows, cols);
                $scope.globalrows.rows = rows;
                $scope.cols = cols;
            });
        }
    };
})

globalrows数组

globalrows数组数据结构

Pankaj Parkar

当前,您的指令未创建任何子范围,基本上它在页面之间共享相同的范围。并且,当您更新任何一个表的数据时,它将同时更新两个地方的数据。

通过具有隔离作用域(如)来创建指令scope: { ... },以便每个指令都可以作为单独的组件工作,并将数据从隔离作用域属性传递给指令。这里data1data2是将由该指令的消费者提供动态表数据值。在您的情况下,您可以做的是,应该从指令中取出服务调用controller,因为这会使您的指令更紧密地耦合在一起Component而是将其移出并放置在指令元素所在的父控制器中。

标记

<grid-print section="1" columns="cols" table-data="data1"></grid-print>
<grid-print section="2" columns="cols" table-data="data2"></grid-print>

控制器

 // If its simple hardcoded data arriving from method then use below
 // $scope.globalrows = dataservice.getCollectionData();
 // $scope.data1 = $scope.globalrows[0];
 // $scope.data2 = $scope.globalrows[1];
 // If data is comming from promise, the do following thing.
 dataservice.getCollectionData().then(function(data){
       $scope.globalrows = data;
       $scope.data1 = $scope.globalrows[0];
       $scope.data2 = $scope.globalrows[1];
});

$scope.cols = $scope.columns; //this would be array of columns.

指示

.directive("gridPrint", function($sessionStorage, dataservice) {
    return {
        restrict: 'E',
        templateUrl: "components/print/templates/print.dir.html",
        replace: true,
        scope: {
           tableData: '=', //table data will set by directive consumer
           columns: '=' // no need for named attributes
        },
        link: function(scope, element, attributes){
            // do DOM manipulation here if required.            
        }
        controller: function($scope) {
            // It's possible the data is not filled yet, because you gain the data from a service (which is asynchronous), 
            //so just initialize with empty array
            if(angular.isUndefined($scope.tableData)){
              $scope.tableData = [];
            }
            if(angular.isUndefined($scope.columns)){
              $scope.columns = [];
            }
        }
    };
})

模板:print.dir.html

<form class="form-horizontal clearfix">
<table class="grid table table-bordered">
  <thead>
    <tr>
      <th ng-repeat="col in columns">{{col.title}}</th>
    </tr>
  </thead>
  <tbody>
      <tr ng-repeat="row in tableData track by $index" index={{index}}>
        <td ng-repeat="col in columns">          
          <span>{{ row[col.field] }}</span>
        </td>  
      </tr>
  </tbody>
</table>
</form>

普伦克在这里

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用ng-repeat嵌套自定义指令

来自分类Dev

如何为ng-table创建自定义指令(对表和ng-repeat使用不同的数据)

来自分类Dev

如何为ng-table创建自定义指令(对表和ng-repeat使用不同的数据)

来自分类Dev

使用自定义指令进行ng-repeat数据绑定

来自分类Dev

使用自定义指令进行ng-repeat数据绑定

来自分类Dev

如何使用ng-repeat生成自定义html标签或指令?

来自分类Dev

在自定义指令中使用ng Repeat来填充选择下拉列表

来自分类Dev

在 AngularJS 中的 ng-repeat 指令中使用自定义过滤器订购数据

来自分类Dev

使用自定义指令,ng-show和$ scope在Angularjs中隐藏/显示元素

来自分类Dev

在自定义replace指令上使用ng-show不会显示或隐藏

来自分类Dev

使用自定义指令,ng-show和$ scope在Angularjs中隐藏/显示元素

来自分类Dev

收到TypeError:在ng-repeat中使用自定义指令时,无法读取未定义的属性“ friends”

来自分类Dev

使用ngFor处理自定义指令-Angular2

来自分类Dev

在自定义指令上使用ng-model和自定义属性进行2种方式绑定之间的区别

来自分类Dev

在自定义指令上使用ng-model和自定义属性进行2种方式绑定之间的区别

来自分类Dev

ANgularjs:ng-repeat和嵌套的自定义指令

来自分类Dev

带templateUrl和ng-repeat的自定义指令

来自分类Dev

带templateUrl和ng-repeat的自定义指令

来自分类Dev

ANgularjs:ng-repeat和嵌套的自定义指令

来自分类Dev

有角度的。内含ng-repeat的自定义指令

来自分类Dev

ng-messages不会显示在自定义指令中

来自分类Dev

AngularJs使用来自自定义Json的ng-repeat创建表

来自分类Dev

AngularJs使用来自自定义Json的ng-repeat创建表

来自分类Dev

Watch无法在另一个自定义元素指令中使用的自定义属性指令中工作

来自分类Dev

如何使用其他自定义指令中的元素标签作为另一个自定义指令的模板

来自分类Dev

在AngularJS中使用带有自定义指令的不同控制器?

来自分类Dev

在Ionic 2中,如何创建使用Ionic组件的自定义指令?

来自分类Dev

如何使用动态名称创建Angular 2自定义属性指令?

来自分类Dev

Angular 2 使用动态 <ng-content> 自定义自动完成的显示

Related 相关文章

  1. 1

    使用ng-repeat嵌套自定义指令

  2. 2

    如何为ng-table创建自定义指令(对表和ng-repeat使用不同的数据)

  3. 3

    如何为ng-table创建自定义指令(对表和ng-repeat使用不同的数据)

  4. 4

    使用自定义指令进行ng-repeat数据绑定

  5. 5

    使用自定义指令进行ng-repeat数据绑定

  6. 6

    如何使用ng-repeat生成自定义html标签或指令?

  7. 7

    在自定义指令中使用ng Repeat来填充选择下拉列表

  8. 8

    在 AngularJS 中的 ng-repeat 指令中使用自定义过滤器订购数据

  9. 9

    使用自定义指令,ng-show和$ scope在Angularjs中隐藏/显示元素

  10. 10

    在自定义replace指令上使用ng-show不会显示或隐藏

  11. 11

    使用自定义指令,ng-show和$ scope在Angularjs中隐藏/显示元素

  12. 12

    收到TypeError:在ng-repeat中使用自定义指令时,无法读取未定义的属性“ friends”

  13. 13

    使用ngFor处理自定义指令-Angular2

  14. 14

    在自定义指令上使用ng-model和自定义属性进行2种方式绑定之间的区别

  15. 15

    在自定义指令上使用ng-model和自定义属性进行2种方式绑定之间的区别

  16. 16

    ANgularjs:ng-repeat和嵌套的自定义指令

  17. 17

    带templateUrl和ng-repeat的自定义指令

  18. 18

    带templateUrl和ng-repeat的自定义指令

  19. 19

    ANgularjs:ng-repeat和嵌套的自定义指令

  20. 20

    有角度的。内含ng-repeat的自定义指令

  21. 21

    ng-messages不会显示在自定义指令中

  22. 22

    AngularJs使用来自自定义Json的ng-repeat创建表

  23. 23

    AngularJs使用来自自定义Json的ng-repeat创建表

  24. 24

    Watch无法在另一个自定义元素指令中使用的自定义属性指令中工作

  25. 25

    如何使用其他自定义指令中的元素标签作为另一个自定义指令的模板

  26. 26

    在AngularJS中使用带有自定义指令的不同控制器?

  27. 27

    在Ionic 2中,如何创建使用Ionic组件的自定义指令?

  28. 28

    如何使用动态名称创建Angular 2自定义属性指令?

  29. 29

    Angular 2 使用动态 <ng-content> 自定义自动完成的显示

热门标签

归档