-2

I think this is a very simple problem but i'm not able to figure it out.

I have 2 controllers in my angularjs file..

analyzer.controller('AnalyzerController',function($scope,$http)
        {
            $scope.builds = [];
            $http.get('/List').success(
                            function(data) {
                                $scope.builds = data.responseData;
                                });

            $scope.showbuild=function(item){
                alert(item);
            }

    });




analyzer.controller('Information',function($scope,$http)
        {
            $scope.cases = [];
            $http.get('/info').success(
                            function(data) {
                                $scope.cases = data.responseData;
                                });

                });

I want to send the value of item in the first controller to the 2nd controller . Item is the value user selects from a box of html page.

Shashank
  • 166
  • 1
  • 2
  • 12

2 Answers2

0

Set state like this

$stateProvider

.state('page1', {
    url: '/page1/:id/:name'
});

in Your Controller, inject $stateParams

then you can get data like

$scope.id=$stateParams.id;
$scope.name=$stateParams.name;
Shashank Agrawal
  • 25,161
  • 11
  • 89
  • 121
Gopinath Kaliappan
  • 6,929
  • 8
  • 37
  • 60
0

There are two main ways: 1) with a factory or service, or 2) broadcasting.

Factories or a Services are both singletons that can be injected into controllers. By creating one of these your can make you data available through the service in both controllers.

app.service('commonService', function() {

    var data = {};

    this.getData = function () {
        return data;
    }

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

});

app.controller("ControllerOne", function($scope, commonService) {
     commonService.setData({key:"value"});
});

app.controller("ControllerTwo", function($scope, commonService) {
     $scope.data = commonService.getData();
});

Alternatively you could broadcast from one controller's scope to another. This solution often entails injecting the rootscope and broadcasting down to another controller which is listening for that broadcast.

something like:

// firing an event downwards
$rootscope.$broadcast('myCustomEvent', {
  someProp: 'Sending you an Object!' // send whatever you want
});

// listen for the event in the relevant $scope
$scope.$on('myCustomEvent', function (event, data) {
  console.log(data); // 'Data to send'
});

This is, in my opinion, a messy solution and I always use services or factories for this sort of thing.

Jeremythuff
  • 1,518
  • 2
  • 13
  • 35