Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

when I submit a form I have a controller that communicates with a service, which gets data from a server and return these data to the controller. Meanwhile, I change view with the form action. My problem is that I'd need the server response data in the second view (which has an another controller), but these are undefined. How can I fix this problem?

Ps. I'm sorry for my English

// code...
.state('app.search', {
  url: "/search",
  views: {
    'menuContent' :{
      templateUrl: "templates/search.html",
          controller: 'SearchCtrl'
    }
  }
})

.state('app.result', {
  url: "/result",
  views: {
    'menuContent' :{
      templateUrl: "templates/result.html",
      controller: "ResultCtrl",
     }
  }
})
// code...

<form name="search_form" ng-submit="searchLines()" action="#/app/result" novalidate>
  // code...
</form>

.factory('Bus', function($http){
return{
    get: function(callback){ 
        $http({
            method: "POST",
            url: "someUrl",
            headers: {'Content-Type': 'application/x-www-form-urlencoded'}, 
            data: {someData from the form}
        })
        .success(function(data) { 
            callback(data);
        })
        .error(function(data, status, error) { 
            console.log(data, status, error);
        });
    }
}
});

.controller('SearchCtrl', function($scope, Bus){
    $scope.searchLines = function(){
        Bus.get(function(data){
          $scope.company = data.company; // this is ok
        }); 
     };
})

.controller('ResultCtrl', function($scope, Bus){
    // I'd like to have $scope.company here
})
share|improve this question

1 Answer 1

Use a service for that.
Controllers shouldn't hold state.
Two controllers can communicate via a service(the service will hold the state as well).

Add a company variable on the Bus service and add a getter so other controllers can fetch the data from Bus.

Example:

HTML:

<div ng-app="app">
    <div ng-controller="aCtrl">{{model.stateA}}</div>
    <div ng-controller="bCtrl">{{model.stateB}}</div>
</div>

JS:

var app = angular.module('app', []);

app.service('myService', function ($q) {
    var state = "12312";

    this.getStateFromServer = function () {
        return $q.when(state);
    }

    this.getRealState = function(){
        return state;
    };
});

app.controller('aCtrl', function ($scope, myService) {
    myService.getStateFromServer().then(function (res) {
        $scope.model = {
            stateA: "A" + myService.getRealState()
        };
    });
});


app.controller('bCtrl', function ($scope, myService) {
    $scope.model = {stateB: "B" + myService.getRealState()};
});

JSFIDDLE.

share|improve this answer
    
In this way I get the same result...it's because I've putted Bus.set() into a $scope function? ex. $scope.searchLines = function(){ Bus.set(departure, arrival, time1, time2, date) .then(function (res) { $scope.company = { stateA: "A" + Bus.get() }; }); –  user4244274 Nov 12 '14 at 15:51
    
And I get this error too: "Cannot read property 'then' of undefined" –  user4244274 Nov 12 '14 at 16:08
    
link –  user4244274 Nov 12 '14 at 16:25
    
I've modified your code a little.. jsfiddle.net/uzygnq7f –  Amir Popovich Nov 12 '14 at 16:37
    
With "$scope.model = {stateB: "B" + Bus.get()}; console.log($scope.model);" I only get "B"...can't figure out –  user4244274 Nov 12 '14 at 17:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.