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 do some request.
function PeopleController($scope,$http,$location){ $http.get('/location/-79.18925/43.77596'). success(function(data){ $scope.savePeopleResponse(data); $location.url('/test'); }); }
Then in the view that gets loaded for /test
I am just calling
<div ng-controller="resultController"> <div class="blueitem">{{getResultForPeople()|json}}</div> </div>
[resultController]
function resultController($scope){ $scope.getResultForPeople = function(){ return $scope.getPeopleResponse(); } }
and the savePeopleResponse and getResultForPeople are "cached" in the rootScope as such
app.run(function($rootScope) { var peopleResponse = {}; $rootScope.savePeopleResponse = function(data) { peopleResponse = data; console.log(data); } $rootScope.getPeopleResponse = function(){ return peopleResponse; } });
Now as you can see, this will get very messy if this application grows larger and larger. What's the best way to handle data so that it's persisted across controllers?
Photo by the author. In this article, we’re going to explore five ways to pass data between View Controllers, with Swift code snippets and examples. The five ways are: 1. Segues Segues are a storyboard mode to pass data. Imagine your app has an onboarding screen, and you want to ask the user's name to address them later on another screen:
@property (nonatomic, assign) BOOL isSomethingEnabled; in ViewControllerA you need to tell it about ViewControllerB so use below #import "ViewControllerB.h" Then where you want to load the view, for example, didSelectRowAtIndex or some IBAction, you need to set the property in ViewControllerB before you push it onto the navigation stack.
Control + click the UI element you are going to use to make the bridge and drag to the second View Controller. Select the “Show” option from the “Action Segue” menu. Control + click the button and drag to the second ViewController, then select “Show.” Now, on the first ViewController class, you need to override the prepare (for segue) method: 2.
You can just create a property in viewcontrollerA. Create an object of viewcontrollerA in viewcontrollerB and assign the desired value to that property. You can also create custom delegates for this.
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With