我有一个搜索控制器和一个类别控制器和一个共享属性工厂。在我的类别控制器中,我设置了一个sharedproperties.property,但我无法在搜索控制器中检索数据,它似乎是空数据,这里是我的代码
.factory('sharedProperties', function () {
var property = '';
return {
getProperty: function () {
return property;
},
setProperty: function(value) {
property = value;
}
};
})
.controller("SearchCtrl",['config','sharedproperties',function(config,sharedproperties)
{
$scope.search_subCategory_businesses= function()
{
$scope.bus_es = sharedProperties.getProperty();
console.log("sharedProperty hahahaah is",sharedProperties.getProperty()); //logs empty instead of data
angular.forEach($scope.bus_es,function(value,key)
{
$scope.business = value;
if($scope.business.logo == '' || $scope.business.logo==null)
{
$scope.business.logo=config.BaseImageURL+"uploads/defbanner.png"
}else
{
$scope.business.logo=config.BaseImageURL+$scope.business.logo;
}
this.push($scope.business);
},$scope.businesses);
};
//$scope.search_resultsFunction();
$scope.search_subCategory_businesses();
}])
.controller("CategoryCtrl",['config','sharedproperties',function(config,sharedproperties)
{
$scope.getBusinesses=function(sub_category_id)
{
$.get($scope.BaseURL+"classes/util.php?sub_category_id="+sub_category_id+"&transaction=get_businesses",function(results){
alert("results are"+JSON.parse(results));
$scope.bus_es =JSON.parse(results);
sharedProperties.setProperty($scope.bus_es);//successfully sets property
console.log("sharedProperty is",sharedProperties.getProperty());
window.location.href=BaseURL+"search.php";
});
}
}])
答案 0 :(得分:0)
您正在混合使用jQuery回调和Angular Promises。你应该通过$ http调用BackEnd并使用promises。 $ http:https://docs.angularjs.org/api/ng/service/ $ http
的文档
.factory('sharedProperties', function () {
var property = '';
return {
getProperty: function () {
return property;
},
setProperty: function(value) {
property = value;
}
};
})
.controller("SearchCtrl",['config','sharedproperties',function(config,sharedproperties)
{
$scope.search_subCategory_businesses= function()
{
$scope.bus_es = sharedProperties.getProperty();
console.log("sharedProperty hahahaah is",sharedProperties.getProperty()); //logs empty instead of data
angular.forEach($scope.bus_es,function(value,key)
{
$scope.business = value;
if($scope.business.logo == '' || $scope.business.logo==null)
{
$scope.business.logo=config.BaseImageURL+"uploads/defbanner.png"
}else
{
$scope.business.logo=config.BaseImageURL+$scope.business.logo;
}
this.push($scope.business);
},$scope.businesses);
};
//$scope.search_resultsFunction();
$scope.search_subCategory_businesses();
}])
.controller("CategoryCtrl",['$http','config','sharedproperties',function($http,config,sharedproperties)
{
$scope.getBusinesses=function(sub_category_id)
{
$http.get($scope.BaseURL+"classes/util.php?sub_category_id="+sub_category_id+"&transaction=get_businesses").then(function(results){
alert("results are"+JSON.parse(results));
$scope.bus_es =JSON.parse(results);
sharedProperties.setProperty($scope.bus_es);//successfully sets property
console.log("sharedProperty is",sharedProperties.getProperty());
window.location.href=BaseURL+"search.php";
});
}
}])
更好的做法是在负责与后端通信的服务中进行这些http调用。