我遇到此错误:未捕获TypeError:b。$ apply不是函数每次单击“编辑”按钮时(应触发指令ng-click但它显示上面的错误) )
以下是docs
这是html
<div class="card">
<div class="card-header">
<h1>Retenciones</h1>
</div>
<div class="card-body card-padding">
<div >
<table ng-if="retencionesController.authorized" dt-instance="retencionesController.dtInstance" datatable="" dt-options="retencionesController.dtOptions" dt-columns="retencionesController.dtColumns"
class="row-border hover"></table>
</div>
</div>
</div>
这是我的控制器
(function(){
angular.module('statusTrackAppApp').controller('retencionesController',retencionesController);
function retencionesController($http,apiPath,DTOptionsBuilder, DTColumnBuilder,$compile){
var vm=this;
vm.authorized = false;
vm.dtInstance = {};
vm.retencion ={};
$http.get(apiPath+'retenciones').then(function(response){
if(response.status ==200){
vm.authorized=true;
var facturas=response.data;
vm.dtOptions = DTOptionsBuilder.newOptions()
.withOption('data', facturas)
.withOption('createdRow', createdRow)
// .withOption('scrollY', 300)
.withPaginationType('full_numbers');
vm.dtColumns = [
DTColumnBuilder.newColumn('_id').withTitle('ID').notVisible(),
DTColumnBuilder.newColumn('noFactura').withTitle('Número de Factura'),
DTColumnBuilder.newColumn('importe').withTitle('Importe'),
DTColumnBuilder.newColumn('tipoMoneda').withTitle('Tipo de Moneda'),
DTColumnBuilder.newColumn('updates').withTitle('Comentarios/Updates'),
DTColumnBuilder.newColumn(null).withTitle('Acciones').notSortable().renderWith(actionButtons)
];
}
}).catch(function(error){
console.log(error);
});
function actionButtons(data,type,full,meta) {
vm.retencion[data._id]=data;
return '<button class="btn btn-info btn-sm" ng-click="retencionesController.editProject(retencionesController.retencion[\'' +data._id+ '\'])">'+
'Editar</button>'
}
function createdRow(row, data, dataIndex) {
// Recompiling so we can bind Angular directive to the DT
$compile(angular.element(row).contents())(vm);
}
vm.editProject = function(retencion){
console.log('it works'+ JSON.parse(retencion));
vm.dtInstance.reloadData();
}
}
})();
btw我用ui-router
将控制器添加到视图中答案 0 :(得分:1)
找到解决方案,我应该使用$ scope而不是vm(它的值是&#39;这个&#39;)
<强>替换强>
function createdRow(row, data, dataIndex) {
// Recompiling so we can bind Angular directive to the DT
$compile(angular.element(row).contents())(vm);
}
使用此
function createdRow(row, data, dataIndex) {
// Recompiling so we can bind Angular directive to the DT
$compile(angular.element(row).contents())($scope);
}