Connect the incoming http request information to a different controller

My navigation menu has the following structure:

<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 using routes to load templates and make ajax requests to fetch data for the templates. The data returned includes an id which I want to bind to gameId, but I'm not sure how to do it.

Here is the JavaScript code for this functionality:

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 want to pass the $scope.gameId = data._id; line from the Game controller to the AdminNav controller. How can I achieve this data binding between the two controllers?

Answer №1

To efficiently manage the game data, you have several options:

1) Set the game ID using $rootScope.gameId = data._id

2) Utilize a service to handle the data adding and retrieving

3) Implement a $broadcast to $on mechanism for data communication

4) Utilize local storage for storing the game ID

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

In the AdminNav Controller, retrieve the stored value with:

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

For more information on defining $localstorage, refer to

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Looking for a specific text on $location update in AngularJS

Is there a way in AngularJS to search the DOM for text after a new view is loaded? The filter almost does this, but I'm not interested in returning a new array of elements. For instance; html <div ng-view=""> <ul> &l ...

Renewing individual parental resolution cache

Is there a way to update specific resolves in the parent resolves without refreshing the entire page? Can resolve caching be disabled for certain items in ui-router? In my current setup, the company object is passed to 2 child states by an abstract parent ...

Ways to prevent decreasing the value below zero in ReactJS?

I have created two buttons, one for increasing and another for decreasing a counter value. However, when I click on the minus button, it should not display negative values. But in my case, when I click on the minus button (initially at zero), it shows -1, ...

How can you determine whether the CSS of a <div> is defined in a class or an id using JavaScript/jQuery?

Hey there, I'm currently working on dynamically changing the CSS of a website. Let's say we have a div like this: <div id="fooid" class="fooclass" ></div> The div has a class "fooclass" and an ID "fooid", and we're getting its ...

The button similar to Facebook does not function properly when using fancybox

My photo gallery uses fancybox, and when fancybox is open a like button appears outside the title. However, the Facebook like button doesn't seem to work. Here is my JavaScript code: $(".fancyboxi").fancybox({ padding: 0, openE ...

Fluidly insert or delete elements

Is there a way to retrieve deleted elements from the DOM after using the jquery .remove function? I have a scenario where I am removing elements from the DOM, but now I'm wondering if it's possible to bring back those deleted elements without hav ...

What is the default state of ng-switch in AngularJS?

I am completely new to using AngularJS and I have a question about setting a default ng-switch state in the following Plunkr. Currently, I can only get it to work when clicking, but ideally the menu should be displayed automatically if the user button is t ...

Adding an element to a blank array using Angular

When attempting to add a new item to an empty array, I encounter an error: $scope.array.push is not a function. However, when the array is not empty, the push operation works flawlessly and updates my scope correctly. Just so you know, my array is initia ...

Utilizing AngularJS and Bootstrap popover: A guide to effectively implementing "popover-is-open" within an ng-repeat loop

When utilizing ng-repeat to generate a table with items, each item has a button in its row that opens a popover for editing the object. I want to avoid using the click-outside property of the bootstrap popover because I only want the popover to close when ...

Adjusting Image Width with jQuery on Hover

I am trying to create a hover effect for two images placed side by side. When the user hovers over one of the images, it should expand. HTML: <a href="#"><div id="skinny"></div></a> <a href="#"><div id="room9"></div ...

It appears that the pixel sizes are different than what was originally designed

In my project, I have a header panel where the burger button is designed with a width of 32px and height of 24px. .header { display: flex; font-weight: bold; font-size: 50px; height: 100px; border: 1px solid black; position: relativ ...

AngularJS scope refreshed following search form validation in Symfony2

Hey there, fellow developers! We are currently faced with the task of rewriting a software application in Symfony2 using AngularJS. We have chosen to utilize Symfony2 for its robust MVC capabilities and AngularJS for its powerful functions. Our current c ...

AngularJS: How to detect the browser's refresh event

Can AngularJS detect when the user clicks the Refresh button on the browser? Is there a built-in method in the framework for developers to access? Thank you ...

Revolving mechanism within React.js

I am currently developing a lottery application using React.js that features a spinning wheel in SVG format. Users can spin the wheel and it smoothly stops at a random position generated by my application. https://i.stack.imgur.com/I7oFb.png To use the w ...

Can you incorporate a custom JavaScript function into an ASP.NET AJAX function?

Creating a dynamic webpage, I have incorporated four buttons that toggle the visibility of specific div elements. As part of my design, I want to invoke a custom function before initiating an AJAX request. This function will smoothly animate the current ...

Setting the timezone and display name format from the user settings into the database: A step-by-step guide

I am currently developing a multi-tenant application utilizing Web API 2.0, MVC 5, and Angular 1 for client-side scripting. Within the application, each tenant is able to personalize their display name format (either FirstName + LastName or LastName + Firs ...

Guide on sharing Photo Blogs on Tumblr using the "tumblr.js" NodeJS module

I've been using the tumblr.js node module to interact with the Tumblr API, but I'm having trouble understanding what exactly should be included in the "options" when posting on my blog. So far, I've only used this module to retrieve my follo ...

Utilizing Filters to Dynamically Highlight and Unhighlight HTML in Angular

Currently, I am experimenting with creating a series of filters that can dynamically highlight and remove highlighting from generated HTML: Highlight filter: app.filter('highlight', function ($sce) { return function (str, termsToHighlight) ...

Is there a better way to implement an inArray function in JavaScript than using join and match?

Recently, I created an inArray function for JavaScript which seems to be working fine. It's short and a bit unusual, but I have a nagging feeling that there might be something wrong with it, although I can't quite pinpoint what it is: Array.prot ...

JavaScript for Acrobat

I am currently creating a form in Adobe Acrobat and incorporating additional functionality using JavaScript. I have been searching for information on the control classes for the form, such as the member variables of a CheckBox, but haven't found any c ...