Better design for passing data to other ng-view's and persisting it across controllers

后端 未结 1 476
忘掉有多难
忘掉有多难 2020-12-02 13:45

I started developing in AngularJS. I\'m confused as to whether this is a proper design to pass data between my partial views.

Right now I have a loader page where I

相关标签:
1条回答
  • 2020-12-02 14:17

    You can persist data across controllers by creating your own service as described nicely in this blog. You can also refer to this question.

    In your case you can move your savePeopleResponse and getPeopleResponse into a service and then inject the service into any controllers you would like to access it.

    angular.module('myApp', [])
        .factory('peopleService', function () {
            var peopleResponse = {};
    
            return {
                savePeopleResponse:function (data) {
                    peopleResponse = data;
                    console.log(data);
                },
                getPeopleResponse:function () {
                    return peopleResponse;
                }
            };
        });
    

    With your controller something like this:

    function resultController ($scope, peopleService) {
        $scope.getResultForPeople = peopleService.getPeopleResponse;
    }
    

    With this code example make sure you include ng-app="myApp"

    0 讨论(0)
提交回复
热议问题