在指令angularJS中调用自定义服务

时间:2019-05-13 16:25:14

标签: angularjs

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>

代码不执行。当我从注入的服务内部指令调用它时,找不到错误“ getGeneraSettings”函数。我是新手,有任何帮助将不胜感激。 enter image description here

1 个答案:

答案 0 :(得分:1)

服务需要返回其自身的实例。

# stuff I need running as root here. Then below runs a bash shell as "developer"
sudo -u developer -H bash -c "$@;"