Search code examples
angularjsangular-servicesangular-directive

Can I call a Service from a Directive?


I have the following service:

myapp.service('myService', function () {
var vm = this;

vm.returnChoreList = function () {
    console.log('returnChoreList : ' + vm.chore.length);
    return vm.chore;
}});

Here is my directive:

myapp.directive("applyplugin", function (myService) {
return {
    link: function (scope, element, attr) {
        scope.$watch(function () {
            $(element).bootstrapSwitch();
        });
        $(element).bind("switchChange.bootstrapSwitch", function (event, state) {
            if (state == true) {

                        var clistArr = myService.returnChoreList;
                        console.log(clistArr.chore.length);
            }
        })
    }
}
});

I want to be able to use the chore array in my directive. But I'm having difficulties getting it.

How can I get this array so I can use it in the directive?


Solution

  • You're missing the parentheses on the function call. Try: var clistArr = myService.returnChoreList();