我正在开发一个应用程序,该应用程序在前端使用 AngularJS,在后端使用 Java (Spring)。该应用程序发出请求 http://localhost:8080/Sprint002/restpatent/
所以我同时运行 Apache 和 Tomcat(端口 80 上的 Apache)。
返回错误 TypeError: Cannot read property 'fetchAllUsers' of undefined
。所以我能看到的是没有找到对象。 Apache和Tomcat同时运行有问题吗,如果没有,是什么原因?
app.controller('UserController', ['UserService', function($scope, UserService) {
var self = this;
self.user={id:null,username:'',address:'',email:''};
self.users=[];
self.submit = submit;
self.edit = edit;
self.remove = remove;
self.reset = reset;
console.log('hello')
fetchAllUsers();
function fetchAllUsers(){
console.log('hello2');
UserService.fetchAllUsers()
.then(
function(d) {
self.users = d;
},
function(errResponse){
console.error('Error while fetching Users');
}
);
}
app.factory('UserService', ['$http', '$q', function($http, $q){
console.log('hello3')
var REST_SERVICE_URI = 'http://localhost:8080/Sprint002/restpatent/';
var factory = {
fetchAllUsers: fetchAllUsers,
createUser: createUser,
updateUser:updateUser,
deleteUser:deleteUser
};
return factory;
function fetchAllUsers() {
var deferred = $q.defer();
console.log('hello4')
$http.get(REST_SERVICE_URI)
.then(
function (response) { //success
deferred.resolve(response.data); //processes the data
},
function(errResponse){ //error
console.error('Error while fetching Users');
deferred.reject(errResponse); //rejects the data
}
);
return deferred.promise;
}
[{"id":1,"applicationNumber":"N8167625","clientRef":"TER-34421","costToRenew":"534","renewalDueDate":"30/09/2017","basketStatus":"Cannot add to Basket"},{"id":2,"applicationNumber":"W71679234","clientRef":"Breitling","costToRenew":"5367","renewalDueDate":"30/11/2017","basketStatus":"Add to Basket"},{"id":3,"applicationNumber":"T7167921","clientRef":"Rolex","costToRenew":"8341","renewalDueDate":"30/05/2017","basketStatus":"In Basket"},{"id":4,"applicationNumber":"uk167444","clientRef":"Timex","costToRenew":"8341","renewalDueDate":"30/02/2017","basketStatus":"Add to Basket"}]
最佳答案
改变这个:
app.controller('UserController', ['UserService', function($scope, UserService)
为此:
app.controller('UserController', ['$scope', 'UserService', function($scope, UserService)
关于javascript - 无法读取未定义的属性 - 代码或服务器有问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43998471/