2014-11-25 1 views
0

나는 초보자입니다. 여기에는 성공에서받은 응답을 다른 컨트롤러로 가져 가야하는 시나리오가 있습니다. 아래 코드를 시도했지만 목표를 달성 할 수 없습니다.한 컨트롤러에서 다른 컨트롤러로 resposne을 가져 오는 방법

코드 :

  $scope.received = function(){ 

       $http({ 
        url : "/generic/getdata", 
        method : 'GET', 
        }).success(function(data) { 
        //The data received here I need t take to mydataController 
        $location.path('/success'); 

       }) 

       when('/success', { 
       templateUrl: 'ngtemplates/success/success.html', 
       controller: 'mydataController' 
       }). 
     app.controller('mydataController', 
    [ '$scope', '$http',function($scope, $http,$location) { 
      //I want the success data here in some function 
      }]); 

제발, 나를

답변

0

당신이 당신의 목적을 위해 서비스를 사용할 수 있습니다 도움이됩니다.

서비스 :

myApp.factory('dataService', function() { 

var _data; 

this.setData = function(someData) { 
_data = someData; // better use angular.copy() function 
} 

return { 
    data : _data; 
    } 
}); 

HTTP의 CALL :

$http({ 
url : "/generic/getdata", 
method : 'GET', 
}).success(function(data) { 
//The data received here I need t take to mydataController 

// before using dataService, make sure you inject it to the controller 
dataService.setData(data); 

$location.path('/success'); 

}); 

CONTROLLER

app.controller('mydataController', 
    [ '$scope', '$http',function($scope, $http,$location, dataService) { 
      //I want the success data here in some function 

     var someData = dataService.data; 
    }]); 
0

당신은 2 초이 olutions. 당신은 서비스를 만들거나 이벤트를 사용할 수 있습니다. mydataController에서

$http({ 
    url : "/generic/getdata", 
    method : 'GET', 
    }).success(function(data) { 
    $rootScope.$broadcast('dataReceived', data); 
    $location.path('/success'); 
}); 

: 당신은 둘 사이의 데이터를 공유 할 수있는 서비스를 만들 수 있습니다

$rootScope.$on('dataReceived', function(e, data) { 
// do something with the data 
} 

또는. mydataController에서

$http({ 
     url : "/generic/getdata", 
     method : 'GET', 
     }).success(function(data) { 
     myDataService.setData(data); 
     $location.path('/success'); 
    }); 

:

$scope.something = myDataService.data; 
제어기에

angular.module('demo').service('myDataService', function() { 

this.data = null; 

this.setData = function(data) { 
this.data = data; 
} 

}); 

관련 문제