Angular JS,工厂 returns 数据,但在控制器中它不见了
Angular JS, factory returns data, but in controller it is gone
我已经开始了解 AngularJS。我设法创建了一些简单的控制器,但我想做一些重构并创建一个工厂。
这是我的一个 js 文件:
angular
.module('myApp', ['ngResource'])
.factory('UserFactory', ['$http', function ($http) {
return {
getOne: function () {
$http({method: 'GET', url: 'http://localhost:8080/login/login'})
.success(function (data, status, headers, config) {
console.info(data);
return data;
})
.error(function (data, status, headers, config) {
alert("failure");
});
}
}
}])
.controller('UserController', ['$scope', 'UserFactory', function ($scope, UserFactory) {
$scope.user = UserFactory.getOne();
}]);
该程序正确地将数据打印到控制台从该行的 servlet 接收到的数据:console.info(data);
但是控制器对视图没有 return 任何东西。我也把 console.info(UserFactory.getOne())
放在这里,它打印 undefined
.
我的编码错误在哪里?
你的工厂 return 在错误的地方,所以它 return 未定义。您可以通过在控制器本身中处理成功来修复它。试试这个。
var app = angular.module('plunker', []);
app.factory('UserFactory', ['$http', function ($http) {
return {
getOne: function (callback) {
$http({method: 'GET', url: 'request_url'})
.success(function (data, status, headers, config) {
callback(data, status, headers, config);
})
.error(function (data, status, headers, config) {
alert("failure");
});
}
}
}]);
app.controller('UserController', ['$scope', 'UserFactory', function ($scope, UserFactory) {
UserFactory.getOne(function(data){
$scope.user = data;
});
}]);
正在工作 URL : DEMO
getOne
需要 return 一个值,在本例中它将是来自 $http 的承诺。成功方法应该 return 控制器所需的值。
.factory('UserFactory', ['$http', function ($http) {
return {
getOne: function () {
return $http({method: 'GET', url: 'http://localhost:8080/login/login'})
.success(function (data, status, headers, config) {
return data;
});
}
}
}]);
现在您可以在您的控制器中处理该结果。
app.controller('UserController', ['$scope', 'UserFactory', function ($scope, UserFactory) {
UserFactory.getOne().then(function(data){
$scope.user = data;
});
}]);
success
和 failure
方法已弃用。您应该尝试改用 then
。
.factory('UserFactory', ['$http', function ($http) {
return {
getOne: function () {
return $http({method: 'GET', url: 'http://localhost:8080/login/login'})
.then(function (response) {
return response.data;
});
}
}
}]);
我已经开始了解 AngularJS。我设法创建了一些简单的控制器,但我想做一些重构并创建一个工厂。
这是我的一个 js 文件:
angular
.module('myApp', ['ngResource'])
.factory('UserFactory', ['$http', function ($http) {
return {
getOne: function () {
$http({method: 'GET', url: 'http://localhost:8080/login/login'})
.success(function (data, status, headers, config) {
console.info(data);
return data;
})
.error(function (data, status, headers, config) {
alert("failure");
});
}
}
}])
.controller('UserController', ['$scope', 'UserFactory', function ($scope, UserFactory) {
$scope.user = UserFactory.getOne();
}]);
该程序正确地将数据打印到控制台从该行的 servlet 接收到的数据:console.info(data);
但是控制器对视图没有 return 任何东西。我也把 console.info(UserFactory.getOne())
放在这里,它打印 undefined
.
我的编码错误在哪里?
你的工厂 return 在错误的地方,所以它 return 未定义。您可以通过在控制器本身中处理成功来修复它。试试这个。
var app = angular.module('plunker', []);
app.factory('UserFactory', ['$http', function ($http) {
return {
getOne: function (callback) {
$http({method: 'GET', url: 'request_url'})
.success(function (data, status, headers, config) {
callback(data, status, headers, config);
})
.error(function (data, status, headers, config) {
alert("failure");
});
}
}
}]);
app.controller('UserController', ['$scope', 'UserFactory', function ($scope, UserFactory) {
UserFactory.getOne(function(data){
$scope.user = data;
});
}]);
正在工作 URL : DEMO
getOne
需要 return 一个值,在本例中它将是来自 $http 的承诺。成功方法应该 return 控制器所需的值。
.factory('UserFactory', ['$http', function ($http) {
return {
getOne: function () {
return $http({method: 'GET', url: 'http://localhost:8080/login/login'})
.success(function (data, status, headers, config) {
return data;
});
}
}
}]);
现在您可以在您的控制器中处理该结果。
app.controller('UserController', ['$scope', 'UserFactory', function ($scope, UserFactory) {
UserFactory.getOne().then(function(data){
$scope.user = data;
});
}]);
success
和 failure
方法已弃用。您应该尝试改用 then
。
.factory('UserFactory', ['$http', function ($http) {
return {
getOne: function () {
return $http({method: 'GET', url: 'http://localhost:8080/login/login'})
.then(function (response) {
return response.data;
});
}
}
}]);