2

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);`
3
  • use $rootScope.abc='your value' , and you can that value in other controller. Commented Sep 22, 2016 at 11:36
  • I m new in angularjs.So pleses Explain it proper wey so i implement it.In my coad Commented Sep 22, 2016 at 11:44
  • do you tried this myService.setJson(Data)? maybe $scope problems? Commented Sep 22, 2016 at 11:53

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

9 Comments

@ashwin Kumbhare , this answer is helpful to you?
Trying to implement
@ashwinKumbhare , if it helps to you then don't forgot to appreciate my work. And mark my answer as a accpt , so it can also helpful to other
i have tried with this one however still not able to get the data in second controller.
@ashwinKumbhare , are you sure? becuase this solution is working in my 3-4 project
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.