我想创建一个表指令,我想知道如何传递回调,例如:"调用不在控制器中的删除函数",我想使用参数作为回调传递,但有一段时间我被它击中了头。当我可以通过"链接"返回回调时,我不能返回值,控制器也不能。
当我使用链接时的图像,但不背回调的值
使用控制器时的图像
查看仪表板
<div crud-table options='vm.options' callback='vm.myCallBack(response)'></div>
控制器仪表板
(function() {
'use strict';
angular
.module('senhalivre')
.controller('DashboardMikrotikCtrl', DashboardMikrotikCtrl);
DashboardMikrotikCtrl.$inject = [];
function DashboardMikrotikCtrl() {
var vm = this;
vm.myCallBack = myCallBack;
vm.options = {
enabled : true,
msg : 'DashBoard',
// Configs etc.....
}
//////////////////
function myCallBack(response){
console.log('Callback !!! ');
console.log(response);
}
}
})();
命令
(function() {
'use strict';
angular
.module('senhalivre')
.directive('crudTable', crudTable);
crudTable.$inject = [];
function crudTable() {
var directive = {
bindToController: true,
controller: crudTableCtrl,
templateUrl : './views/crud/crudTable.html',
controllerAs: 'vm',
link: link,
restrict: 'EA',
scope: {
options : '=',
callback : '&callback'
}
};
return directive;
////////////////
function link(scope, element, attrs, ctrl) {
scope.vm.callback({test : 'something'});
// Work
}
}
crudTableCtrl.$inject = [];
function crudTableCtrl() {
var vm = this;
vm.callback({test : 'something'});
// :(
}
})();
调用回调时,您需要在参数对象中提供属性"response":
function link(scope, element, attrs, ctrl) {
scope.vm.callback({response: 'some response text'});
}
或
function crudTableCtrl() {
this.callback({response: 'something'});
}