我有一個搜索控制器和一個類別控制器和一個共享屬性工廠。在我的類別控制器我設置sharedproperties.property但我無法檢索搜索控制器的似乎是空的數據的數據,這裏是我的代碼無法將角度js中的數據從一個控制器移動到另一個控制器
.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";
});
}
}])
它看起來不像'sharedProperties'包含在你的控制器中。 –
您需要將服務注入您的控制器。現在你只注入'配置'? –
以及我剛剛忘了發佈時的問題,但他們被注入 – henrybbosa