简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM