[英]Return value from factory service to controller in AngularJs is lost
我想将列表对象从工厂服务返回到控制器。 当我从服务返回数据时,列表对象包含数据,但是当控件到达调用控制器时,列表将为空(值丢失)。
我在文本输入中称ng-change =“ search()”
这是代码:
factoryservice.js
app.factory('newService', ['$http', '$q','$filter', function ($http, $q, $filter)
{
var ServiceFactory = {};
var searchMatch = function (haystack, needle) {
if (!needle) {
return true;
}
return haystack.toString().toLowerCase().indexOf(needle.toLowerCase()) !== -1;
};
var _search = function (Items, data, sortingOrder, query, reverse) {
Items = $filter('filter')(data, function (item) {
for (var attr in item) {
if (angular.isUndefined(item[attr]) || item[attr] === null)
continue;
if (searchMatch(item[attr], query))
return true;
}
return false;
});
// take care of the sorting order
if (sortingOrder !== '') {
Items = $filter('orderBy')(Items, sortingOrder, reverse);
}
return Items;
};
ServiceFactory.search = _search;
return ServiceFactory;
}]);
在此,搜索方法中的“返回项目”具有适当的值。
controller.js
app.controller('mycontroller', ['$scope', '$http', '$filter', 'Service', function ($scope, $http, $filter, Service) {
var sortingOrder = 'Site';
$scope.sortingOrder = sortingOrder;
$scope.reverse = false;
$scope.Items = [];
$scope.Data = function () {
$scope.loading = true;
$http({
url: "http://localhost:50100/api/mycontroller",
dataType: 'json',
method: 'GET',
data: '', // data:'', for input parameters, create a small entity class and assign it
headers: {
"Content-Type": "application/json"
// "Content-Type": 'application/x-www-form-urlencoded'
}
}).success(function (response) {
$scope.Data = response;
$scope.Items = $scope.Data;
})
.error(function (error) {
alert(error);
})
.finally(function () {
$scope.loading = false;
});
}
$scope.search = function () {
return Service.search($scope.Items, $scope.Data, $scope.sortingOrder, $scope.query, $scope.reverse);
};
}]);
在这里,$ scope.search不包含任何值(这里的数据丢失)。
我是angularJS的新手,希望有人可以帮助我。 提前致谢。
您需要将服务名称( newService
not Service
)注入到控制器中,例如:
app.controller('mycontroller', ['$scope', '$http', '$filter', 'newService', function ($scope, $http, $filter, newService) {
因此,您的$scope.search
函数应修改为:
$scope.search = function () {
return newService.search($scope.Items, $scope.Data, $scope.sortingOrder, $scope.query, $scope.reverse);
};
如果上述方法不起作用,我可以仔细查看您的服务设置。
您的$scope.Data
函数将自己替换为成功处理函数。
$scope.Data = function () {
$scope.loading = true;
$http({
url: "http://localhost:50100/api/mycontroller",
dataType: 'json',
method: 'GET',
data: '', // data:'', for input parameters, create a small entity class and assign it
headers: {
"Content-Type": "application/json"
// "Content-Type": 'application/x-www-form-urlencoded'
}
}).success(function (response) {
//THIS IS A PROBLEM
$scope.Data = response;
$scope.Items = $scope.Data;
})
.error(function (error) {
alert(error);
})
.finally(function () {
$scope.loading = false;
});
}
谢谢朋友们的回复... !!!
我只是更改了下面的代码行,它似乎正在工作。
$ scope.search = function(){$ scope.search = Service.search($ scope.Items,$ scope.Data,$ scope.sortingOrder,$ scope.query,$ scope.reverse); };
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.