繁体   English   中英

使用$ rootScope返回AngularJS中的json吗?

[英]Use $rootScope to returns json in AngularJS ?

我有这个函数返回$scope.product_owners = data json

$http({
    url: "php/functions.php",
    method: "GET",
    params: { 
        action: "get_product_owners"
    }
}).success(function(data) {
    $scope.product_owners = data;
});  

目前,我正在所有控制器中调用此函数,因为它们已在其中使用,但我想知道是否可以调用一次。 例如,使用$rootScope东西。

跨控制器共享数据的“角度方式”是使用服务

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;
});

然后在每个控制器中注入服务:

app.controller('someCtrl', function (ProductOwners) {
    $scope.product_owners = ProductOwners.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;
    });
});

更新

另一个具有“惰性”评估并支持传递给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;
    });
});

如果您的应用程序中的多个控制器使用相同的代码,则可能希望将其放入服务中,然后可以从控制器中调用它:

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;
        });  
    };
});

然后,在您的控制器中:

myApp.controller('MyCtrl', function($scope, products) {
    products.getProductOwners($scope);
});

使用服务是在多个控制器之间重用代码的首选方式。

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;
}); 
}}

然后基本上将它注入到任何地方

yourApp.controller('xCtrl', function (httpFactory) {
    $scope.product_owners = httpFactory.product_owners;
});

2-您也可以为该应用程序提供主控制器

<body ng-controller="mainCtrl">

然后将您的代码放在其中

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;
}); }

现在您可以从任何子范围访问此数据

您绝对可以将其放在$rootScope以保存多个调用。

问题是-如果将一个呼叫放在一个控制器上,是否可以转到其他视图/控制器,从而跳过该呼叫?

如果答案是否定的,那么您没有问题。

如果答案是肯定的,那么您应该让控制器包装所有其他控制器,以确保您拥有该数据。

另一种可能性是提供一种服务来保留product_owners数据,并且每个控制器都可以访问该服务器以获取数据,如果不可用,则通过ajax请求获取它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM