0
angular.module("quickquiz-builder").service("SettingsService", function ($http, $q) {
    var self = {
        'generalSettings': [],
        'getGeneralSettings' : function(){
            var d = $q.defer();
            $http({
                url: "get.php",
                    method: "GET"
                }).then(function successCallback(resonse) {
                    var config = resonse.data.config;
                    config = JSON.parse(config).data;
                    self.generalSettings = config.settings;
                    d.resolve();
                }, function errorCallback(response) {
                    console.dir(response);
            });
            d.promise();
        }
    }
}), angular.module("quickquiz-builder").directive("quizbuilderSettings", ["SettingsService", "QuestionsService", "$filter", function (a, b, c) {
    return {
        restrict: "E",
        scope: {},
        templateUrl: "templates/settings.html",
        controllerAs: "ctrl",
        controller: ["$scope", function (c) {
            a.getGeneralSettings().then(function success(data){
                console.dir(data);
        });
        }]
    }
}])
<div ng-app="quickquiz-builder">
    <div ng-view></div>
</div>

The code does not executes. I get error 'getGeneraSettings' function not found when i call it from injected service inside directive. I am newbie in angular, any help will be a great appreciation. enter image description here

1 Answer 1

1

The service needs to return an instance of itself.

angular.module("quickquiz-builder").service("SettingsService", function ($http, $q) {
   const self = this;
   self.foobar = function(){};
   return self;
});
Sign up to request clarification or add additional context in comments.

1 Comment

Hi @doublesharp, your answer is good and working as expected. But I faced a different situation after this. Please see following question: stackoverflow.com/questions/56247558/… I will be grateful if you can help me..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.