Angular `ng-click` not working in DataTables table row

后端 未结 3 828
悲哀的现实
悲哀的现实 2021-01-13 15:40

I have an angular application that is also using jquery.dataTables. When I use datatables to build a dynamic table with th

3条回答
  •  感动是毒
    2021-01-13 15:52

    $compile takes in a snippet of HTML and returns what's known as a linking function. This function takes a $scope that will it will use to do all the databinding.

    This might have been confusing since you are using the controller as syntax (which is a good thing), so you don't deal directly $scope.

    The two things you need to do here are to inject both $compile and $scope into your controller, and then use them.

    //Using array injector notation here
    app.controller('myCtrl', 
    ['$scope','$compile',
      function($scope, $compile) {
         //snip...
      }
    ]);
    

    And then later when you are linking your row, you can call it with the injected $scope like this:

    $compile(angular.element(row).contents())($scope);
    

    If you run the snippet below, you can see it all works as expected.

    var app = angular.module('appy', []);
    app.controller('myCtrl', ['$scope','$compile',
      function($scope, $compile) {
        var _this = this;
    
        $('#report').DataTable({
          data: [{
            "LastName": "Doe",
            "Link": "
    div {
        margin-top: 15px;
        padding: 5px;
      }
      div.borderdiv {
        border: 1px solid black;
      }
      td {
        border: 1px solid black;
        padding: 2px
      }
      .success {
        background-color: green;
      }
    
    
    
    
    
    

    Button with ng-click

    HTML Table with ng-click

    Last Name Actions
    Doe

    DataTables with ng-click

提交回复
热议问题