简体   繁体   English

将 JSON 数据从一个控制器传递到另一个控制器 angularJS

[英]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();我想将数据从一个控制器传递到另一个控制器,我正在$scope.imagegallerys变量中获取数据,并且我能够将数据设置为SetJson();

controller 1控制器 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.我想将数据发送到另一个控制器,但是 getJson() 没有返回任何值或对象。

controller 2控制器 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我希望这个解决方案对你有帮助,如果没有请告诉我

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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