我是angularJS的新手
我使用带有ui-router和slickgrid的angularJS 我尝试通过单击图像调用方法来调用Web服务并删除行
我有这个控制器:
app.controller('customerController', ['$scope', '$compile', 'CustomerService', function ($scope, $compile, CustomerService) {
// function to get customers
$scope.getCustomers = function () {
CustomerService.getCustomers($("#userId").val(), $scope.getArchived).then(function (customers) {
dataView.beginUpdate();
dataView.setItems(customers);
dataView.endUpdate();
grid.invalidate();
}, function (reason) {
noty({ text: reason, type: 'error' });
});
}
// function to archive customer
$scope.archiveCustomer = function(customerId) {
if (confirm(translate("LBL_DELETE_CUSTOMER_CONFIRMATION"))) {
CustomerService.archiveCustomer(customerId).then(function () {
$scope.getCustomers();
}, function (reason) {
noty({ text: reason, type: 'error' });
});
}
return false;
}
// create grid
var dataView,
grid;
var options = {
enableCellNavigation: true,
enableColumnReorder: false,
autoHeight: true
};
var columns = [
{ id: "id", name: "", field: "id", width: 20, formatter: deleteFormatter, cssClass: "centeredColumn" },
{ id: "customer", name: translate("Customer"), field: "customer", width: 250 }
];
function deleteFormatter(row, cell, value, columnDef, dataContext) {
html = "<img src='images/delete.gif' alt='" + translate("LBL_DELETE_CUSTOMER") + "' style='cursor:pointer;' ng-click='archiveCustomer(" + dataContext.id + ");' />";
linker = $compile(angular.element(html));
htmlElements = linker($scope);
html = htmlElements[0].outerHTML;
return html;
}
dataView = new Slick.Data.DataView({ inlineFilters: true });
grid = new Slick.Grid("#customersGrid", dataView, columns, options);
$scope.getCustomers();
}]);
我的问题是ng-click由deleteFormatter添加的img元素。 怎么了?我看到了带有指令的样品,但在我看来并不清楚......你能点亮我吗?
修改
尝试使用asyncPostRender但未成功: 列定义:
var columns = [
{ id: "id", name: "", field: "id", width: 20, formatter: asyncFormatter, asyncPostRender: deleteRender, cssClass: "centeredColumn" },
{ id: "customer", name: translate("Customer"), field: "customer", width: 250 }
];
格式化并渲染:
function asyncFormatter(row, cell, value, columnDef, dataContext) {
return "Data loading...";
}
function deleteRender(cellNode, row, dataContext, colDef) {
html = "<img src='images/delete.gif' alt='" + translate("LBL_DELETE_CUSTOMER") + "' style='cursor:pointer;' ng-click='archiveCustomer(" + dataContext.id + ");' />";
linker = $compile(angular.element(html));
htmlElements = linker($scope);
$(cellNode).empty()
cellNode.innerHTML = htmlElements[0].outerHTML;
}
答案 0 :(得分:1)
<强>已更新强>
&#34;格式化&#34;似乎只是给定列的配置函数。您需要为该列使用渲染函数,例如&#34; asyncPostRender&#34;。请参阅this example。
您的新元素(删除img)超出任何Angular范围。你需要在网格行的渲染事件中将它编译成Angular。
答案 1 :(得分:1)
在Kursad Gulseven(谢谢你)和this post
的帮助下回答控制器声明:
app.controller('customerController', ['$scope', '$rootScope', '$compile', '$interpolate', 'CustomerService', function ($scope, $rootScope, $compile, $interpolate, CustomerService) {
列定义:
var columns = [
{ id: "id", name: "", field: "id", width: 20, formatter: deleteFormatter, asyncPostRender: deleteRender, cssClass: "centeredColumn" },
{ id: "customer", name: translate("Customer"), field: "customer", width: 250 }
];
格式化并渲染:
function deleteFormatter(row, cell, value, columnDef, dataContext) {
return "<img src='images/delete.gif' alt='delete' style='cursor:pointer;' ng-click='archiveCustomer(" + dataContext.id + ");' />";
}
function deleteRender(cellNode, row, dataContext, colDef) {
// compile the html with angular to get ng-click with the correct scope
var interpolated = $interpolate($(cellNode).html())($scope);
var linker = $compile(interpolated);
var htmlElements = linker($scope);
$(cellNode).empty()
$(cellNode).append(htmlElements);
}
归档客户功能:
$scope.archiveCustomer = function (customerId) {
if (confirm(translate("LBL_DELETE_CUSTOMER_CONFIRMATION"))) {
CustomerService.archiveCustomer(customerId).then(function () {
$scope.getCustomers();
}, function (reason) {
noty({ text: reason, type: 'error' });
});
}
}