ashwin Kumbhare
ashwin Kumbhare

Reputation: 21

passing JSON data from one controller to another controller angularJS

i want to pass the data from one controller to another controller, i am Getting Data in $scope.imagegallerys variable and i am able to set the data into SetJson();

controller 1

function imageGalleryController($scope,$http,myService){
    var urlBase = "http://localhost:8084/Apc/api/";
    $http.get(urlBase + '/gallerylist').
    success(function(Data) {
    $scope.imagegallerys = Data;
    myService.setJson($scope.imagegallerys);
 });    
}

I want get data to anther controller however getJson() is not returning any value or object.

controller 2

    function categoryController($scope,myService){
    $scope.myreturn = myService.getJson();
    }

Factory

    function myService(){
      var imagegallerys = null;//the object to hold our data
      return{
      getJson:function(){
      return imagegallerys;
      },
      setJson:function(value){
      imagegallerys = value;
       }
   }

angular.module('Abc')
.controller('categoryController', categoryController)
.controller('imageGalleryController',imageGalleryController)
.factory('myService',myService);`

Upvotes: 2

Views: 671

Answers (1)

chirag satapara
chirag satapara

Reputation: 1947

This is your first controller:

function imageGalleryController($scope,$http,myService,$rootScope){
    var urlBase = "http://localhost:8084/Apc/api/";
    $http.get(urlBase + '/gallerylist').
    success(function(Data) {
    $rootScope.imagegallerys = Data;
    myService.setJson($scope.imagegallerys);
 });    
}

And below is your second controller:

function categoryController($scope,myService,$rootScope){
    console.log($rootScope.imagegallerys);
    $scope.myreturn = myService.getJson();
    }

I hope this solution help you , If not please inform me

Upvotes: 1

Related Questions