10

I'm building a website using angularjs and i'm getting data from a webservice. I need to populate that data to a datatable and create an edit button for each row. After some investigation i came up with this

The problem is that the ng-click isn't working probably because i need to compile the html i injected to the table cell. I've tried that in several ways but unfortunately i'm still very new to angular and i don't seem to understand how i can accomplish that. I really need help with this one.

This is my directive:

dialogApp.directive('myTable', function ($compile) {
return {
    restrict: 'E, A, C',
    link: function (scope, element, attrs, controller) {
        var dataTable = element.dataTable(scope.options);

        scope.$watch('options.aaData', handleModelUpdates, true);

        function handleModelUpdates(newData) {
            var data = newData || null;
            if (data) {
                dataTable.fnClearTable();
                dataTable.fnAddData(data);
            }
        }
    },
    scope: {
        options: "="
    }
};}); 

The controller:

dialogApp.controller('DataTableTestController', ['$scope', function($scope){
$scope.coisas = "coisas";
$scope.botaoEdit = function(a){
    console.log(a);
};

$scope.options = {
    "sDom": '<"H"lf>t<"F"ip>',
    "bStateSave": true,
    "bPaginate": false,
    "bLengthChange": false,
    "bFilter": true,
    "bSort": true,
    "bInfo": true,
    "bAutoWidth": false,
    "sPaginationType": "full_numbers",
    aoColumns: [{
        "sTitle": "name"
    }, {
        "sTitle": "price"
    }, {
        "sTitle": "category"
    }, {
        "sTitle": "action"
    }, null],
    "aaSorting": [[ 0, "asc" ]],
    aoColumnDefs: [
        { "bSortable": true, "aTargets": [0] },
        { "bSortable": false, "aTargets": [1,2,3,4] }
    ],
    bJQueryUI: true,
    aaData: []
};

var dbStuff = [
    {
        "name": "Stuff1",
        "price": 10000000.00,
        "description": "Expensive Stuff",
        "wanna":"buy"
    },
    {
        "name": "Stuff2",
        "price": 20000000.00,
        "description": "Oh my...",
        "wanna":"have money"
    }
]

for (var key in dbStuff){
    $scope.options.aaData.push([dbStuff[key].name,
                                dbStuff[key].price,
                                dbStuff[key].description,
                                dbStuff[key].wanna,
                                "<button ng-click=\"botaoEdit("+dbStuff[key].name+")\">test button</button>"
                               ]);
}

$scope.counter = 0; }])

And the HTML:

<link type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.12/themes/redmond/jquery-ui.css" rel="stylesheet" />
<link rel="stylesheet" type="text/css" href="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.8.2/css/jquery.dataTables.css">

<div ng-app="tableExample">
    <div ng-controller="DataTableTestController">   
        {{ coisas }}
        <table my-table options="options" class="jquery-datatables"></table>
    </div>
</div>
4

1 回答 1

22

是的,你是对的,ng-click 指令不是由 Angular 编译的。所以最直接的方法是使用 onclick 监听器:

 "<button onclick=\"angular.element(this).scope().botaoEdit('"+dbStuff[key].name+"')\">test button</button>"

不要忘记添加引号:botaoEdit(''),在您的小提琴中您尝试访问 Stuff1 变量:)

最后,我认为最好的方法是使用一些网格插件或 ng-repeat,它会在收到数据时为您的表构建行。在这种方法中,您的行中的 ng-click 可以正常工作。

于 2014-03-10T14:28:27.960 回答