我创建了一个简单的指令,用于显示我正在创建的<table>
的排序列标题。
ngGrid.directive("sortColumn", function() {
return {
restrict: "E",
replace: true,
transclude: true,
scope: {
sortby: "@",
onsort: "="
},
template: "<span><a href='#' ng-click='sort()' ng-transclude></a></span>",
link: function(scope, element, attrs) {
scope.sort = function () {
// I want to call CONTROLLER.onSort here, but how do I access the controller scope?...
scope.controllerOnSort(scope.sortby);
};
}
};
});
这是一个创建表头的例子:
<table id="mainGrid" ng-controller="GridCtrl>
<thead>
<tr>
<th><sort-column sortby="Name">Name</sort-column></th>
<th><sort-column sortby="DateCreated">Date Created</sort-column></th>
<th>Hi</th>
</tr>
</thead>
当单击排序列时,我希望在我的网格控制器上触发onControllerSort函数..但是我卡住了!到目前为止,我能够做到的唯一方法是为每个<sort-column>
添加“ onSort”属性,并在指令中引用这些属性:
因此,当点击排序列时,我希望能够在我的网格控制器上触发onControllerSort函数。但是我遇到了困难!目前为止,我唯一能够做的就是为每个<sort-column>
添加"onSort"属性,并在指令中引用这些属性:
<sort-column onSort="controllerOnSort" sortby="Name">Name</sort-column>
但这并不好,因为我总是想调用 controllerOnSort,所以在每个指令中都添加它有点丑陋。有没有办法在不需要在HTML中添加不必要的标记的情况下,在指令内部实现这个功能?如果可以的话,该指令和控制器都在同一个模块中定义。
$parent
是一种不好的模式。它只能在更深层次上工作一次,因此很难重复使用。 - Carlos Morales'='
的双向绑定已经过时,并且会使迁移到 Angular 2+ 更加困难。有关更多信息,请参见 AngularJS 开发人员指南 - 基于组件的应用程序架构。 - georgeawg