在对象中定义的 ng-click 中的角度调用函数



有一个关于Angularjs的问题,它是ng-repeat/ng-click工作。

因此,在我正在研究的这个系统上,有很多代码重用于数据表,我正在尝试制作一个通用模板/服务来解决这个问题。现在我遇到了一个问题,我们有多个按钮,在单击时有自己的函数调用。

到目前为止,我已经有了这个设置: 我的列对象定义如下:

var columns = [
{
identifier: "id",
type: "text"
},
{
identifier: "type",
type: "text"
},
{
identifier: "label",
type: "text"
},
{
identifier: "actions",
type: "button",
multi: true,
content: [
{
icon: "fa-globe",
events: {
click: $scope.openMapModal
}
},
{
icon: "fa-list",
events: {
click: $scope.openGroupModal
}
}
]
}
];

和我的 HTML 如下:

<tr ng-repeat="row in table.data" ng-model-instant>
<td ng-repeat="column in table.columns" ng-if="column.type === 'text'">
{{TableService.getByString(row, column.identifier)}}
</td>
<td ng-repeat="column in table.columns" ng-if="column.type === 'button' && column.multi">
<a ng-repeat="button in column.content" class="btn fa {{button.icon}}" ng-click="button.events.click(row)"></a>
</td>
</tr>

为了完整起见,我的 TableService.getByString 和一个小的表数据集: (请注意,上面定义的列是由函数设置到表对象中的,我没有将其包含在对象中(。

var table = {
data: [
{
id: 0,
label: "foo",
type: "bar"
},
{
id: 1,
label: "one",
type: "bar"
},
{
id: 2,
label: "foo",
type: "two"
}
]
}
function getKeyObj(obj, key) {
var retVal = {
"key": key,
"obj": obj
};
if (retVal.key.indexOf('.') > -1) {
var keyParts = retVal.key.split('.');
retVal.key = keyParts.pop();
while (keyParts.length && (obj = obj[keyParts.shift()])) ;
retVal.obj = obj;
}
return retVal;
}
function getByString(obj, key) {
var ret = getKeyObj(obj, key);
return ret.obj[ret.key];
}

现在我遇到的问题是,每当我单击按钮时,我的函数都不会在我的 ng-click 中被调用。

我也尝试过将函数设置为我的列对象中的字符串,但它也没有用。

我是否朝着正确的方向前进,或者我是否需要重新考虑我的概括,如果是这样,还有什么选择?

您确定在定义列数组时定义了$scope.openMapModal吗?可能是您正在将未定义分配给 click 属性而不是对函数的引用?

最新更新