簡體   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