Angularjs 错误:无法获取属性';呼叫';指未定义的或空的引用
我正在使用AngularJs。从controller.js到service.js获取数据时,我得到了错误。以下是使用的代码: //controller.jsAngularjs 错误:无法获取属性';呼叫';指未定义的或空的引用,angularjs,Angularjs,我正在使用AngularJs。从controller.js到service.js获取数据时,我得到了错误。以下是使用的代码: //controller.js angular.module('testApp.controllers', []). controller('testController', function ($scope, testAPIService, $timeout, $window, $location, $anchorScroll, $http) { $sco
angular.module('testApp.controllers', []).
controller('testController', function ($scope, testAPIService, $timeout, $window, $location, $anchorScroll, $http) {
$scope.show = function() {
testAPIService.getDataSummary().success(function (response) {
console.log(response);
}).error(function (response) {
alert(response.responseText);
});
}
});
In Service.js
angular.module('testApp.services', []).
factory('testAPIService', ['$http', function ($http) {
var testAPIService = {};
testAPIService.getDataSummary = function () {
var request = {
url: urlBase + 'GetDataSummary',
method: 'Get',
headers: {
'accept': 'application/json'
}
}
return $http(request);
};
return testAPIService;
}]);
如何解决这个问题?谢谢您正在创建两个不同的模块:
- 第一个模块
在创建控制器时创建testApp.controllers
- 创建服务时,将创建另一个模块
testApp.services
testApp
模块:
app.js
// With [] means to create a new module.
angular.module('testApp', []);
// Without [] means to retrieve an existing module.
angular.module('testApp').
factory('testAPIService', ['$http', function($http) {
var testAPIService = {};
testAPIService.getDataSummary = function() {
var request = {
url: urlBase + 'GetDataSummary',
method: 'Get',
headers: {
'accept': 'application/json'
}
}
return $http(request);
};
return testAPIService;
}]);
controller.js
// Without [] means to retrieve an existing module.
angular.module('testApp').
controller('testController', function($scope, testAPIService, $timeout, $window, $location, $anchorScroll, $http) {
$scope.show = function() {
testAPIService.getDataSummary().success(function(response) {
console.log(response);
}).error(function(response) {
alert(response.responseText);
});
}
});
service.js
// With [] means to create a new module.
angular.module('testApp', []);
// Without [] means to retrieve an existing module.
angular.module('testApp').
factory('testAPIService', ['$http', function($http) {
var testAPIService = {};
testAPIService.getDataSummary = function() {
var request = {
url: urlBase + 'GetDataSummary',
method: 'Get',
headers: {
'accept': 'application/json'
}
}
return $http(request);
};
return testAPIService;
}]);
index.html
并将您的ng app
指令更改为指向testApp
模块
<html ng-app="testApp">
这可能是在angularjs文件之前包含任何应用程序javascript文件的结果
确保在应用程序文件的其余部分之前包含angularjs文件。在
TestapService
工厂中,TestapService
对象来自何处?始终包含错误的全文,包括堆栈跟踪。Hi@matthew Cawley。抱歉,代码中没有返回语句。它就在那里,我在这里贴的时候错过了。我还更新了现有的代码,如果我在错误后刷新页面,那么当方法被点击时,不会显示错误。只有在第一次命中该方法时,才会显示此错误,并且不会返回任何数据hi@Matthew Cawley。我已经按照你的逻辑更新了代码。我已经对我给出的现有代码块进行了更改。但是仍然是相同的错误在声明服务和控制器时,您是否确实调用了angular.module('testApp')
而不是angular.module('testApp',[])
?缺少[]
很重要。With[]
表示创建模块,而不表示检索现有模块。Hi@Matthew Cawley。如果删除[],则在加载初始页面时,不会加载任何控件,并出现以下错误:SCRIPT5022:[$injector:nomod]模块“testApp.controllers”不可用!您要么拼错了模块名,要么忘记加载它。如果注册模块,请确保将依赖项指定为第二个参数。文件:angular.js,行:2075,列:11Hi@Claudia。在加载自定义js文件之前,首先加载Angular.js文件。是否在声明的行上声明testAPIService
,testAPIService.getDataSummary=function(){
或之前类似的testAPIService={}
然后转到前面提到的行。我的意思是,您发布的代码是您工厂的完整代码?我想说的是,在为其分配getDataSummary
属性时,谁是testAPIService
?它一定是以前声明过的。我说得对吗?嗨@claudia。我还有其他方法,所以我只给出了api服务声明为var testAPIService={};然后是方法。我已经更新了初始代码