簡體   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