заправить сервис в контроллере в angularjs - PullRequest
0 голосов
/ 08 октября 2018

У меня проблемы с введением моего moment сервиса в мой MainController, как показано ниже.Я не уверен, что понимаю, как лучше всего выполнить инъекцию.

Дайте мне знать, если мне нужно предоставить больше контекста или информации.Заранее спасибо за помощь.

MainController.js:

define(['../appconfig'],
function (appconfig) {
    'use strict';

    function MainController($scope,$rootScope,$http) {
        var vm = this;
        var getDivId = document.getElementById("demo");

        dataFactory.successCB();/*I want use service.js method this way**/
        dataFactory.errorCB(); /*I want use service.js method this way**/

        vm.getLocation = function() {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(showPosition,showError);
        } 
        else { 
            getDivId.innerHTML = "Geolocation is not supported by this browser.";
            }
        }
        /**
        * MOCK DATA FOR WEATHER API.
        **/
        function getMockData() {
            var data = {
                "coord":{"lon":139,"lat":35},
                "sys":{"country":"JP","sunrise":1369769524,"sunset":1369821049},
                "weather":[{"id":804,"main":"clouds","description":"overcast clouds","icon":"04n"}],
                "main":{"temp":289.5,"humidity":89,"pressure":1013,"temp_min":287.04,"temp_max":292.04},
                "wind":{"speed":7.31,"deg":187.002},
                "rain":{"3h":0},
                "clouds":{"all":92},
                "dt":1369824698,
                "id":1851632,
                "name":"Shuzenji",
                "cod":200
            };

            return data;
        } 

        function showPosition(position){            

            var obj = getMockData();

             angular.forEach(obj.weather, function (item) {
                getDivId.innerHTML += item.main;
            });

        }

        function showError(error) {
            switch(error.code) {
                case error.PERMISSION_DENIED:
                    var getLocation = prompt("Please enter your Location");

                    var obj = getMockData();

                     angular.forEach(obj.weather, function (item) {
                        getDivId.innerHTML += item.main;
                    });

                    break;
                case error.POSITION_UNAVAILABLE:
                    getDivId.innerHTML = "Location information is unavailable."
                    break;
                case error.TIMEOUT:
                    getDivId.innerHTML = "The request to get user location timed out."
                    break;
                case error.UNKNOWN_ERROR:
                    getDivId.innerHTML = "An unknown error occurred."
                    break;
            }
        }
    }
    MainController.$inject = ['$scope', '$rootScope', '$http'];

    return MainController;
});

Service.js:

define(['moment'],
function(moment) {
    'use strict';

    return ['$http', function($http) {
        'use strict';

        successCB: function(){
            console.log("In sucess");
        }

        errorCB: function(){
            console.log("In error");
        }
        return dataFactory;
    }];
});
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...