dev777 dev777 - 1 year ago 88
AngularJS Question

How to send data from one controller to other in angularjs

How to send the json data from FirstCtrl to secondCtrl using angularjs.

Can anyone please help me out regarding this ...


.controller('FirstCtrl', function ($scope) {

//firstCtrl json data
$.getJSON("sample.json", function (json) {


.controller('secondCtrl', function ($scope) {

//get the firstCtrl json data here

Answer Source

I would also suggest a service that gets and returns data from and to the controllers.

we create the two controllers and then we create a service with two functions: 1. one to get the json data 2. one to return the json data

Like so:

'use strict';
angular.module('myApp', [])

.controller('FirstCtrl', function( $scope, myService ){
 //we create or get the json object
 $scope.myjsonObj = {

      //pass the json object to the service

.controller('SecondCtrl', function( $scope, myService ){
        //call service getJson() function to get the data
       $scope.myreturnedData = myService.getJson();
 .factory('myService', function(){
    var myjsonObj = null;//the object to hold our data
     return {
       return myjsonObj;
      myjsonObj = value;


and the HTML partial would be:

 <div ng-app="myApp">
        <div ng-controller="FirstCtrl">
        <div ng-controller="SecondCtrl">

Hope helps, good luck.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download