0

I have two tables in my page, and upon clicking on a row on the first table, i wanted to call an ajax request to update the second table.

I'm trying to do this with two controllers, each of them with an ng-repeat filling the rows with values. I've burned out every neuron i could afford and I'm still stumped.

This is my code

app.controller("TermsCtrl", function($scope, $http) {
    $http.get('json.php').then(function(res) {
        $scope.wordfreq = res.data;
        $scope.decodeURIComponent = decodeURIComponent;
        $scope.unescape = unescape;
    });
    $scope.go = function(id) { // This makes the rows clickable and calls the next results
        return $http.get('json2.php?word=' + id).then(function(result) {
            secondtable = result.data;
            console.log(secondtable); // I see the objects!
            return secondtable;
        });
    };
});
app.controller("TermsCtrl2", function($scope, secondtable) {
    $scope.secondfeq = secondtable;
    console.log(scope.secondfeq); // No dice
});

Any ideas how to get the secondtable results from the click into the TermsCtrl2 controller?

cheers

bronze
  • 79
  • 7
  • what is `secondtable` here? it isn't the name of a service, and it isn't a property on `$scope`, were you trying to declare it as a global variable on window? – Claies Jul 03 '15 at 15:13
  • `secondtable` was supposed to be the data i wanted to take over the the second controller – bronze Jul 03 '15 at 15:17
  • yes, but where is the variable declared? – Claies Jul 03 '15 at 15:29
  • actually nowhere before this, thought it would only come into play after clicking the first table and making the request – bronze Jul 03 '15 at 15:31
  • 1
    well, right now, you are returning `secondtable` after the success of the `$http.get`, which is in turn returning that to be stored in `$scope.go`. Technically, inside the `$scope` of `"TermsCtrl"` you have a `go` property that has your data, but it wouldn't be visible on the `$scope` of `"TermsCtrl2"`, and in fact, your script probably throws an error trying to resolve `secondtable` as an input parameter for your controller. I would say that refactoring the code the way @CT14.IT suggested is the right way to go.... – Claies Jul 03 '15 at 15:44
  • Did you find a solution for this? I am trying to do the exact same thing and I have tried all that I could but still not able to make it work. – Abhi Nov 27 '15 at 04:27
  • Hey Abhi, yes I did! Luckily i put the stackoverflow urls as comments in the code. I believe this may help you: http://stackoverflow.com/questions/31211080/passing-a-var-between-2-controllers-via-a-service-in-angular I also looked up how to pass variables and these 2 urls may help you too: http://stackoverflow.com/questions/23937963/how-to-fire-a-controller-method-from-another-controller-or-directive http://stackoverflow.com/questions/29794897/can-not-share-variables-in-angular-controllers Hit me up if those dont do the trick. – bronze Dec 01 '15 at 00:39

1 Answers1

0

I would suggest that using a service to store data that can be accessed from multiple controllers would be the way to go. You can use dependency injection to define which controllers have access to the service.

The following answers things

How to preserve data through AngularJS routing?

Community
  • 1
  • 1
CT14.IT
  • 1,635
  • 1
  • 15
  • 31