简体   繁体   English

将Javascript对象(DataTable列)传递给Angular指令

[英]Passing a Javascript Object (DataTable Columns) to Angular Directive

I'm trying to derive jquery datatable to create my custom datatable and make the columns dynamic. 我试图派生jquery数据表以创建我的自定义数据表并使列动态。 I can't pass the colums as an object. 我不能将列作为对象传递。 I'm getting a string in the directive controller. 我在指令控制器中得到一个字符串。

    var columns = 
        { 
            cols: [
            {
                "mDataProp": "Name",
                "render": function (data, type, full, meta) {
                    return '<a href="#/u/d/' + full.Id + '">' + full.Name + '</a>';
                }
            },
            { "mDataProp": "prop2" }


    };
    function getColumns() {
        return columns;
    }



<my-table 
          columns='columns'
          message="my message"></my-table>



app.directive('myTable', function () {
    return {
        restrict: 'E',
        templateUrl: '/TableView.html',
        replace: false,
        controller: function ($scope) {
            // $scope.urun = 
           // $scope.arg1 = attrs.headerText;
           //
        },
        scope: {
            columns: '=columns'
        },
        link: function (scope, element, attrs) {

            var columns = attrs.columns;
            console.log(columns);

        }
    }

You need to assign your variable columns to a scope property in your controller and then: 您需要将变量列分配给控制器中的作用域属性,然后:

app.directive('myTable', function () {
    return {
        restrict: 'E',
        templateUrl: '/TableView.html',
        replace: false,
        controller: function ($scope) {
            // $scope.urun = 
           // $scope.arg1 = attrs.headerText;
           //
        },
        scope: {
            columns: '='
        },
        link: function (scope, element, attrs) {

            var columns = $scope.columns;
            console.log(columns);

        }
    }

More info on how scope config works: $compile 有关范围配置如何工作的更多信息: $ compile

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM