我无法将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;
}];
});