2015-12-09 20 views
1

Angular是新手,我已經在我的應用程序中實現了一個指令。該指令有兩個控件,它們調用兩個不同的控制器方法(帶參數)。我怎麼能說這樣的功能,因爲我已經明白指示作爲插件(或排序)。也該參數設置不工作also.This是我的代碼在單個指令中調用兩個不同控件的方法AngularJS

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('') 
} 
}); 

誰能幫我

謝謝& Registers Arjun Menon

+0

用ng-click代替,假設你的功能是在你的ctrl中定義的 – Jax

回答

1

在Angular中,通信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('') 
} 

注意:變更onclickng-click模板內。

+0

非常感謝Avijit,會試試並會讓你知道:) –

相關問題