Share data between controllers

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 = "";
            function browse() {

                .success(function (data, status, headers, config) {


                    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) {


var url = "";

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

Thanks for the precious help!

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
    $scope.$on('myevent', function(event, response) {

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

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

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

Why you are not using a simple ngRepeat?


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


.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