javascript - AngularJS 数据表 Controller 标记

标签 javascript angularjs datatable

最近我一直在 angularJS 上调整这个模块 angular-datatables ajax现在我遇到了这个错误

angular.min.js:117 TypeError: Cannot read property 'aDataSort' of undefined

我刚刚发现在 html 元素中 Controller 定义的标记上, Controller 名称后面有一个“as”关键字,它解决了我的错误。

Now, the question is what is that "as" keyword in that definition and would it be possible not to use it? please refer to the code snippet below

不工作代码:

<div ng-controller="mainCtrl">
    <table datatable="" dt-options="showCase.dtOptions" dt-columns="showCase.dtColumns" class="table table-hover table-bordered table-striped"></table>
</div>

工作代码:

<div ng-controller="mainCtrl as showCase">
    <table datatable="" dt-options="showCase.dtOptions" dt-columns="showCase.dtColumns" class="table table-hover table-bordered table-striped"></table>
</div>

我也尝试了这段代码,但它不起作用

<div ng-controller="mainCtrl">
    <table datatable="" dt-options="dtOptions" dt-columns="dtColumns" class="table table-hover table-bordered table-striped"></table>
</div>

这是我的 Controller 和应用程序 js

var app = angular.module('medrec', ['ngRoute','ngResource','ui.bootstrap','datatables'])
                 .constant('API_URL', window.location.href);

app.controller('mainCtrl',['$scope',
                           'API_URL',
                           'DTOptionsBuilder',
                           'DTColumnBuilder',
                           '$resource',
    function($scope, API_URL, DTOptionsBuilder, DTColumnBuilder, $resource){


        var vm = this;
        vm.dtOptions = DTOptionsBuilder.fromSource(API_URL+'tests/data.json')
            .withPaginationType('simple');
        vm.dtColumns = [
            DTColumnBuilder.newColumn('id').withTitle('ID'),
            DTColumnBuilder.newColumn('firstName').withTitle('First name'),
            DTColumnBuilder.newColumn('lastName').withTitle('Last name').notVisible()
        ];
}]);

最佳答案

当在 Controller 中使用 this 而不是 $sope 时,您应该在 veiw 中使用 controller as 语法。

在 Controller 中:

  var vm = this;

在 View 中

   ng-controller="ctrl as c"

还可以使用 c.dtOptions 访问作用域函数或变量,如示例中所示。

关于javascript - AngularJS 数据表 Controller 标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36963672/

相关文章:

c# - 为什么我需要再次将 DataRow 转换为 DataRow?

javascript - d3.extent 在使用约束松弛绘制时在 x 轴和图形的点标签之间产生碰撞

javascript - 在两个 KonvaJS Canvas 之间拖动形状

javascript - AngularJS:模型更改时如何更改单选按钮 View ?

javascript - ng-required,如何在输入之间使用它?

angularjs - 为 Angular 2 实现 ngClassEven ngClassOdd

javascript - 具有未知行数的数据表服务器端处理

php - 将多行从一个表传输到另一个表

javascript - 运行超时的 Mocha 测试永远不会完成

javascript - node-orm2 实例缺少保存方法