繁体   English   中英

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

[英]passing JSON data from one controller to another controller angularJS

我想将数据从一个控制器传递到另一个控制器,我正在$scope.imagegallerys变量中获取数据,并且我能够将数据设置为SetJson();

控制器 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);
 });    
}

我想将数据发送到另一个控制器,但是 getJson() 没有返回任何值或对象。

控制器 2

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

工厂

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

这是你的第一个控制器:

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

下面是你的第二个控制器:

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

我希望这个解决方案对你有帮助,如果没有请告诉我

暂无
暂无

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

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