在指令和控制器 angularjs 之间传递信息 [英] Pass information between directive and controller angularjs
本文介绍了在指令和控制器 angularjs 之间传递信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有下一个指令
angular
.module('01')
.directive('tableSortable', tableSortable);
/** @ngInject */
function tableSortable() {
return {
restrict: 'E',
transclude: true,
templateUrl: 'app/components/tabla/tableSortable.html',
scope: {
columns: '=',
data: '=',
sort: '=',
onclickRow: '='
}
我有下一个显示表格的 html
and I have the next html that shows a table
<tbody>
<tr ng-click="click(object)" ng-repeat="object in data | orderBy:sort.column:sort.descending | orderBy:sort.column:sort.descending | startFrom:currentPage*pageSize | limitTo:pageSize">
<td
ng-repeat="key in object"
ng-if="key != '$$hashKey'" >
{{object[columns[$index].variable] | customFilter : getFilterOfColumn(columns[$index].variable)}}
</td>
</tr>
</tbody>
我有另一个 html,我在其中调用指令
I have this other html where I call the directive
<table-sortable onclick-row="main.onclickRow(msg)"
columns="main.columnsBusquedas" data="main.rowsBusquedas" sort="main.sortBusquedas">
</table-sortable>
这个带有 onclickRow(msg) 函数的控制器,我想获取用户点击的那一行
And this controller with a function onclickRow(msg) where I want to take the row that the users click on
function onclickRow(msg){
$log.debug(msg);
}
此代码无效...您能帮帮我吗?
This code didn't work... Could you help me please?
谢谢.
推荐答案
改变你绑定函数的指令部分,你应该使用 &
而不是 =
>
Change your directive part where you bind function, you should use &
instead of =
function tableSortable() {
return {
restrict: 'E',
transclude: true,
templateUrl: 'app/components/tabla/tableSortable.html',
scope: {
columns: '=',
data: '=',
sort: '=',
click: '&onclickRow' // this how we call function from directive...
}
在 HTML 中更改您的 ng-click
如下所示..
In HTML change your ng-click
like below..
<tbody>
<tr ng-click="click({'msg':object})" ng-repeat="object in data | orderBy:sort.column:sort.descending | orderBy:sort.column:sort.descending | startFrom:currentPage*pageSize | limitTo:pageSize">
<td
ng-repeat="key in object"
ng-if="key != '$$hashKey'" >
{{object[columns[$index].variable] | customFilter : getFilterOfColumn(columns[$index].variable)}}
</td>
</tr>
</tbody>
这篇关于在指令和控制器 angularjs 之间传递信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文