Reputation: 2463
My directive is to build out a scaffolded table with a delete button. I have a service set up named Post, so when I call the directive I'm including a servicename property on the element. How do I dynamically inject my service based on my service name attribute I'm including? Or is there a better way to do this?
var lassoDirectives = angular.module('lassoDirectives', []);
lassoDirectives.directive('autoTable', ['parse', function(parse) {
return {
restrict: 'AE',
scope: {
data: '=',
modelname: '='
},
templateUrl: 'http://local.angular.com/webroot/app/templates/directives/auto-table.html',
controller: function($scope) {
console.log(parse);
$scope.deleteItem = function(id) {
console.log(id);
// rocket = modelname.query();
//console.log(rocket);
}
$scope.check = function() {
parse.get("Posts", 123, function(response) {
console.log(response.toString());
});
}
}
};
}]);
Upvotes: 3
Views: 2954
Reputation: 4009
I'm not sure that is the best way to go around it. You should probably have a single service that is charge of all the data related to this lassoDirective
, so you could just inject it as normal.
But you may be able to use the $injector
provider like...
.directive('autoTable', function($injector) {
return {
restrict: 'EA',
scope: {
servicename: '='
},
controller: function(scope) {
var service = $injector.get(scope.servicename);
scope.deleteItem = function(Id) {
del = service.delete({Id: Id});
items = service.query();
};
}
}
});
If you want your service to perform differently depending on the directive clicked, it may be better to just pass your service additional parameters relating to the directive? Not sure what your situation is so ignore this common if N/A.
Upvotes: 4