0

I have a nav that looks something like this:

<div class="collapse navbar-collapse" id="admin-side-nav" ng-controller="AdminNav">
    <ul class="nav nav-pills nav-stacked">
        <li><a href="/admin/leaderboard/{{gameId}}">Leaderboard</a></li>
        <li><a href="/admin/newsFeed/{{gameId}}">News Feed</a></li>
    </ul>
</div>

I am then using routes which load a template and with an ajax request to get data to put in the template. The returned request has an id in it that I would like to bind to gameId, but I can't figure out how it is done.

Here is the javascript to do this:

var console = angular.module('Console', ["ngRoute", "highcharts-ng"]);

console.config(function($routeProvider, $locationProvider){
    $routeProvider.when('/admin/:gameid', {
        templateUrl: 'admin/game.html',
        controller: 'Game'
    });
}).controller("Game", function($scope, $http){
    $http.get("http://admin.gmserver.net/mypage").success(function(data){
        $scope.games = data;
        $scope.gameId = data._id;
    });
}).controller("AdminNav", function($scope, $location){
    $scope.isActive = function(viewLocation){
        return viewLocation === $location.path();
    };
});

I would like to bind this line from the Game controller $scope.gameId = data._id; to the value in the AdminNav controller. How can I pass the data from the Game controller to the AdminNav controller?

Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338
  • Potential duplicate: http://stackoverflow.com/questions/20181323/passing-data-between-controllers-in-angular-js – Olivercodes May 17 '15 at 01:51
  • how about moving the `$http` out to a `service` so that it can be used in any `controller`? it might be a good idea to `cache` it too like this: [link](http://stackoverflow.com/questions/14117653/how-to-cache-an-http-get-service-in-angularjs) – Shehryar Abbasi May 17 '15 at 04:32

1 Answers1

0

You will want to use either:

1) $rootScope.gameId = data._id

2) A service that adds and gets the value

3) a $broadcast to $on

4) a localstorage call

$localstorage.setObject('gameId', data._id);

and in the AdminNav Controller:

`var yourNewValue = $localstorage.getObject('gameId');

See http://learn.ionicframework.com/formulas/localstorage/ for defining $localstorage

Olivercodes
  • 1,048
  • 5
  • 17