[英]Use $rootScope to returns json in AngularJS ?
I have this function which returns json
with $scope.product_owners = data
我有这个函数返回$scope.product_owners = data
json
$http({
url: "php/functions.php",
method: "GET",
params: {
action: "get_product_owners"
}
}).success(function(data) {
$scope.product_owners = data;
});
Currently, I'm calling this function in all my controllers because it is used in them, but I'm wondering if it was possible to call once. 目前,我正在所有控制器中调用此函数,因为它们已在其中使用,但我想知道是否可以调用一次。 For example, with the $rootScope
or something like that. 例如,使用$rootScope
东西。
The "Angular way" for sharing data across controllers is to use a service : 跨控制器共享数据的“角度方式”是使用服务 :
app.factory('ProductOwners', function ($http) {
var service = {
data: []
};
$http({
url: "php/functions.php",
method: "GET",
params: {
action: "get_product_owners"
}
}).success(function(data) {
service.data = data;
});
return service;
});
Then inject the service in every controller: 然后在每个控制器中注入服务:
app.controller('someCtrl', function (ProductOwners) {
$scope.product_owners = ProductOwners.data;
});
A different implementation with "lazy" evaluation (ie it only makes the call if it is needed and then serves the same data): 具有“惰性”评估的另一种实现(即,仅在需要时才进行调用,然后提供相同的数据):
app.factory('ProductOwners', function ($http, $q) {
var data;
function getDataIfNeeded() {
if (data !== undefined) {
return $q.when(data);
}
return $http({
url: "php/functions.php",
method: "GET",
params: {
action: "get_product_owners"
}
}).then(function(response) {
data = response.data;
return data;
});
}
return {
getData: getDataIfNeeded
};
});
app.controller('someCtrl', function (ProductOwners) {
ProductOwners.getData().then(function (data) {
$scope.product_owners = data;
});
});
UPDATE 更新
Yet another different implementation with "lazy" evaluation and supporting an argument passed to getData()
: 另一个具有“惰性”评估并支持传递给getData()
的参数的实现:
app.factory('GenericService', function ($http, $q) {
var data = {};
function getDataIfNeeded(action) {
action = action || 'default';
if (data[action] !== undefined) {
return $q.when(data[action]);
}
return $http({
url: "php/functions.php",
method: "GET",
params: {
action: action
}
}).then(function(response) {
data[action] = response.data;
return data[action];
});
}
return {
getData: getDataIfNeeded
};
});
app.controller('someCtrl', function (GenericService) {
GenericService.getData("get_product_owners").then(function (data) {
$scope.product_owners = data;
});
});
If the same code is used by several controllers in your application, you may wish to put it inside a service and then you can call it from the controllers: 如果您的应用程序中的多个控制器使用相同的代码,则可能希望将其放入服务中,然后可以从控制器中调用它:
myApp.service('products', function($http) {
this.getProductOwners = function(targetScope) {
$http({
url: "php/functions.php",
method: "GET",
params: {
action: "get_product_owners"
}
}).success(function(data) {
targetScope.product_owners = data;
});
};
});
And then, in your controllers: 然后,在您的控制器中:
myApp.controller('MyCtrl', function($scope, products) {
products.getProductOwners($scope);
});
Using services is the preferred way for code reuse between several controllers. 使用服务是在多个控制器之间重用代码的首选方式。
1- you can make a factory and then call it when needed inside controllers 1-您可以建立工厂,然后在需要时在控制器内调用它
yourApp.factory('httpFactory', function($http) {
return {
$http({
url: "php/functions.php",
method: "GET",
params: {
action: "get_product_owners"
}
}).success(function(data) {
this.product_owners = data;
});
}}
then basically you inject it in to wherever, 然后基本上将它注入到任何地方
yourApp.controller('xCtrl', function (httpFactory) {
$scope.product_owners = httpFactory.product_owners;
});
2- you can also have a main controller for the app like this 2-您也可以为该应用程序提供主控制器
<body ng-controller="mainCtrl">
and then put your code in that 然后将您的代码放在其中
yourApp.controller('mainCtrl', function($scope, $http) {
$http({
url: "php/functions.php",
method: "GET",
params: {
action: "get_product_owners"
}
}).success(function(data) {
$scope.product_owners = data;
}); }
now you can access this data from any schild scope 现在您可以从任何子范围访问此数据
You can definitely place it on the $rootScope
to save multiple calls. 您绝对可以将其放在$rootScope
以保存多个调用。
Question is - if you place the one call on one controller, is it possible to go to a different view/controller and thus skipping the call ? 问题是-如果将一个呼叫放在一个控制器上,是否可以转到其他视图/控制器,从而跳过该呼叫?
If the answer is no then you have no problem. 如果答案是否定的,那么您没有问题。
If the answer is yes then you should have a controller wrapping all the other ones to make sure you have that data. 如果答案是肯定的,那么您应该让控制器包装所有其他控制器,以确保您拥有该数据。
Another possibility is have a service to keep the product_owners
data, and each controller can access that server to get the data, and if it's not available, get it by the ajax
request. 另一种可能性是提供一种服务来保留product_owners
数据,并且每个控制器都可以访问该服务器以获取数据,如果不可用,则通过ajax
请求获取它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.