我有两个 Angular 模块,starter.constant 用于存储所有常量,starter.services 用于存储 Controller 。
angular.module('starter.services', ['ui.bootstrap', 'ngTouch', 'ngCordova', 'ngStorage', 'starter.core'])
.factory('examService', ["$http", "$ionicLoading", 'BASE_URL', function($http, $ionicLoading) {
return {
httpGet: function(url, data) {
$ionicLoading.show();
return $http.get(BASE_URL + url, data);
},
httpUpdate: function(url, data) {
$ionicLoading.show();
return $http.patch(BASE_URL + url, data);
},
httpPost: function(url, data) {
$ionicLoading.show();
return $http.post(BASE_URL + url, data);
},
httpDelete: function(url, data) {
$ionicLoading.show();
return $http.delete(BASE_URL + url, data);
}
};
}])
常量模块:
(function() {
'use strict';
angular
.module('starter.core',[])
.constant('BASE_URL', 'http://example');
})();
但是,当我进入浏览器时,出现以下错误:
ReferenceError: BASE_URL is not defined
抛出此错误的行位于 httpGet 函数中
有什么建议或进展吗?
最佳答案
您需要将BASE_URL
注入(inject)到您的工厂函数中:
factory('examService', ["$http", "$ionicLoading", 'BASE_URL', function($http, $ionicLoading, BASE_URL) {
关于javascript - ReferenceError : BASE_URL is not defined, Angular JS 依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34561313/