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

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? Commented Jul 3, 2015 at 15:13
  • secondtable was supposed to be the data i wanted to take over the the second controller Commented Jul 3, 2015 at 15:17
  • yes, but where is the variable declared? Commented Jul 3, 2015 at 15:29
  • actually nowhere before this, thought it would only come into play after clicking the first table and making the request Commented Jul 3, 2015 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.... Commented Jul 3, 2015 at 15:44

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.