-2

I've been testing Angularjs for a project and I would like to know how to capture data on a controller from view and pass this data to another controller.

This pseudocode exemplifies what I want to do:

controller1('$scope', function($scope){
    var val1 = $scope.dataFromView;
});

controller2('$scope', function($scope, controller1 ){
    var val2 = controller1.val1;
    //then do something with val1
});
acdcjunior
  • 132,397
  • 37
  • 331
  • 304
Alfredo
  • 65
  • 3
  • 1
    See http://stackoverflow.com/questions/21919962/share-data-between-angularjs-controllers?rq=1 – mccainz Dec 02 '15 at 19:53
  • see this http://stackoverflow.com/questions/33664549/how-to-pass-the-value-from-one-page-to-another-page-in-angular/33664624#33664624 – Upalr Dec 02 '15 at 19:53

1 Answers1

0

You should use factories for this

app.factory('val1', function() {
  // Your code goes here
});

And then in the controller, inject the factory.

app.controller('controller1', ['val1', function(val1) {
}]);

app.controller('controller2', ['val1', function(val1) {
}]);
Richard Hamilton
  • 25,478
  • 10
  • 60
  • 87