我刚接触过角,我在我的应用程序中实现了一个指令。该指令有两个控件,它们调用两个不同的控制器方法(带有参数)。如何调用这些函数,正如我所理解的,指令充当插件(或某种程度上).Also--参数设置不工作-- also.This是我的代码
myDirective.directive('entityActions', function () {
return {
scope: {
entity: '=entityActions'
},
restrict: 'EA',
template: [
'<td>{{entity.EntityType}}</td>',
'<td>{{entity.Description}}</td>',
"<td class=''>",
"<a onclick='editentity(entity.EntityType)' class='btn btn-info btn-xs margin-right4px'>",
"<span class='glyphicon glyphicon-pencil'></span>",
"</a>",
"<a onclick='deleteEntity(entity.EntityType)' class='btn btn-danger btn-xs'>",
"<span class='glyphicon glyphicon-remove'></span>",
"</a>",
"</td>"
].join('')
}
});有人能帮帮我吗?
谢谢和问候阿尔琼·梅农
发布于 2015-12-09 12:17:28
在角,通信b/w指令和控制器必须通过服务/工厂。
因此,让我们为以下内容创建一个服务:
.service('EntityService', function() {
this.editentity = function(type) {}; // define function here instead of controller
this.deleteEntity = function(type) {}; // define function here instead of controller
}); 在现有控制器定义中插入此服务:
.controller('MyCtrl', ['$scope', 'EntityService', function($scope, EntityService) {
$scope.editentity = EntityService.editentity;
$scope.deletEentity = EntityService.deleteEntity;
}]);最后,修改如下指令:
myDirective.directive('entityActions', function () {
return {
scope: {
entity: '=entityActions'
},
restrict: 'EA',
controller: function($scope, EntityService) {
$scope.deleteEntity = function(type) {
EntityService.deleteEntity(type);
};
$scope.editentity = function(type) {
EntityService.editentity(type);
}
},
template: [
'<td>{{entity.EntityType}}</td>',
'<td>{{entity.Description}}</td>',
"<td class=''>",
"<a ng-click='editentity(entity.EntityType)' class='btn btn-info btn-xs margin-right4px'>",
"<span class='glyphicon glyphicon-pencil'></span>",
"</a>",
"<a ng-click='deleteEntity(entity.EntityType)' class='btn btn-danger btn-xs'>",
"<span class='glyphicon glyphicon-remove'></span>",
"</a>",
"</td>"
].join('')
}注意事项:将模板内的onclick更改为ng-click。
https://stackoverflow.com/questions/34178378
复制相似问题