Angularjs工厂服务不将动态数据返回给控制器 [英] Angularjs factory service not returning dynamic data to controller
问题描述
这是我的工厂服务,它从网络服务获取数据
This is my factory service and it is fetching data from a web service
var customersFactory = function ($http) {
var customer = [];
var getCustomersURL = "http://localhost:50340/Services/CustomerService.asmx/GetCustomers";
customer.XMLtoJSON = function (data) {
data = data.replace('<?xml version="1.0" encoding="utf-8"?>', ''); ;
data = data.replace('<string xmlns="http://tempuri.org/">', '').replace('</string>', '');
return $.parseJSON(data);
};
customer.GetCustomers = function () {
$http.post(getCustomersURL).success(function (data, status, headers, config) {
return customer.XMLtoJSON(data);
}).error(function (ata, status, headers, config) { });
};
return customer;
};
app.factory('customersFactory', customersFactory);
现在,这是在我的控制器中使用
Now, this is being used in my controller
app.controller("CustomersController", function ($scope, customersFactory) {
var service = [];
service = customersFactory.GetCustomers();
$scope.Customers = service;
if ((typeof (service) != 'undefined') && service.length > 0) {
service.then(function (result) {
$scope.Customers = result;
});
}
});
服务的值始终未定义或为空。数据未从工厂传递到控制器。我称之为简单的Web服务,没有花哨的API或WCF。
它有一些静态/虚拟数据,它工作正常。控制器正在获取数据并正在显示。
我在哪里做错了?
任意非常感谢帮助。
谢谢
我尝试过:
我是AnuglarJS的新手,并且正在努力获得数据的动态绑定,但如果我放一些静态/虚拟数据,它就可以了。
那么,我在哪里乱搞?代码中还缺少什么。
The value of service is always undefined or empty. Data is not being passed from factory to controller. Im calling a simple web service, no fancy APIs or WCF.
It there was some static/dummy data, its working fine. The controller is fetching the data and is being displayed.
Where am I doing wrong?
Any help is greatly appreciated.
Thank you
What I have tried:
I'm a newbie to AnuglarJS and was struggling to get the dynamic binding of data but if I put some static/dummy data, it works.
So, where am I messing up? What else is missing in the code.
推荐答案
http){
var customer = [ ]。
var getCustomersURL = http://本地主机:50340 /服务/ CustomerService.asmx / GetCustomers的跨度>;
customer.XMLtoJSON = function (data){
data = data.replace(' <?xml version =1.0encoding =utf-8?>',' '); ;
data = data.replace(' < string xmlns =http://tempuri.org/ >',' ')。replace(' < / string>',' '跨度>);
return
http) { var customer = []; var getCustomersURL = "http://localhost:50340/Services/CustomerService.asmx/GetCustomers"; customer.XMLtoJSON = function (data) { data = data.replace('<?xml version="1.0" encoding="utf-8"?>', ''); ; data = data.replace('<string xmlns="http://tempuri.org/">', '').replace('</string>', ''); return
.parseJSON(data);
};
customer.GetCustomers = function (){
.parseJSON(data); }; customer.GetCustomers = function () {
http.post(getCustomersURL).success( function (数据,状态,标题,配置){
return customer.XMLtoJSON(data) ;
})。error( function (ata,status,headers,config){});
};
返回客户;
};
app.factory(' customersFactory',customersFactory);
http.post(getCustomersURL).success(function (data, status, headers, config) { return customer.XMLtoJSON(data); }).error(function (ata, status, headers, config) { }); }; return customer; }; app.factory('customersFactory', customersFactory);
现在,这是在我的控制器中使用
Now, this is being used in my controller
app.controller("CustomersController", function (
这篇关于Angularjs工厂服务不将动态数据返回给控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!