Share data between controllers


#1

Hello,
I’m having a problem which is, i need to send an “id” from a jsonp response and want to use that same id to another contoller and include on the url of the jsonp request something like this:

First Controller:

.controller("MyBrowse", function($scope, $http, $timeout, Idjsonp) {

var url = "http://www.domain.com/lala/search?json=1&callback=JSON_CALLBACK";
            browse();
            function browse() {
            

            $http.jsonp(url)
                .success(function (data, status, headers, config) {

                    console.log(data);

                    var newULElement = angular.element(document.querySelector('#listResult'))
                        var _data = data.lala;
                        for(var index in _data) {
                        $scope.Idjsonp = _data[index].id;

Controller i need to receive the id :

.controller("lala_Description", function($scope, $http, $timeout, $ionicLoading, Idjsonp) {

  
  alert($scope.Idjsonp);
   

var url = "http://www.domain.com/lala/view?id=1498";

i need to insert that id from the first controller in this last url from the secound controller.

Thanks for the precious help!


#2

Of course, this is not the best solution at all.
Since you have to share data between two or more controllers you must think into a Service.

But, you can save the id in $rootScope, an get it.

Even if necessary, broadcast an event to notify all controllers

.controller('Controller1', function($rootScope, $scope, $http) {
    $http.get(url).success(function(response) {
       $rootScope.resourceId = response.resourceId;
       $rootScope.$broadcast('myevent', response);
    }
})
.controller('Controller2', function($rootScope, $scope) {
    //Do whatever with $rootScope.resourceId
    alert($rootScope.resourceId);
    $scope.$on('myevent', function(event, response) {
       alert(response.resourceId);
    });
});

btw (I know my english is so poor, im sorry :(, i just wanted to help :blush: )


#3

thanks @soutlink!!

It’s passing the id :smile:
Now i have a little problem i need to know in my “for” what item is clicked to know what id is correspondent for the next view

something like an : data-id="’ + _data[index].id + '"

My code for this :

for(var index in _data) {
var newLiElement = angular.element('<li><a nav-clear menu-close href="#/app/property" >' + '<span><h2 class="text_title">' + _data[index].id + '<h2></span></a></li>');
                    newULElement.append(newLiElement);
                    }

I don’t know if is the best solution… :confused:
Thank you for the help!!


#4

Why you are not using a simple ngRepeat?

html

<ul ng-controller="Controller2">
    <li ng-repeat="item in items" ng-class="{'active': item.id == selectedItemId}">{{ item.id }}</li>
</ul>

Controller

.controller('Controller2', function($rootScope, $scope) {
    $scope.selectedItemId = $rootScope.resourceId;
   //Load this via ajax or whatever
    $scope.items = [{id: 1, name: 'Item 1}, {id: 2, name: 'Item 2}, ...]; 
});

Its is not good solution at all mix DOM manipulation with angular /ionic