An AngularJS-powered dynamic navbar

Apologies if my explanation is unclear, but I am struggling to find a simple way to convey the following information.

I have set up a navigation bar that displays different categories of articles. These navigation items are pulled from a database and can be edited in the back-office.

To retrieve the categories, I am using an AngularJS controller :

app.controller('ApiController', ['$scope', '$http', function($scope, $http) {
    $http.get('/categories')
        .success(function(categories) {
            $scope.categories = categories;
        });
}]);
<div class="collapse navbar-collapse" id="target-navbar-main">
    <ul class="nav navbar-nav>
        <li ng-repeat="category in categories">
            <a href="/<% category.slug %>" title="<% category.name %>"><% category.name %></a>
        </li>
    </ul>
</div>

In the body of the page, I am utilizing another controller to fetch articles based on the selected category. However, I am facing challenges in establishing communication between the navbar and the articles controller to pass along the category information when a button is clicked.

Your assistance in this matter would be greatly appreciated.

Answer №1

In my opinion, it seems like what you're aiming for in the API is a service rather than just a controller. In Angular, controllers are typically not used to send large amounts of data between each other. Instead, important calls and shared data that multiple controllers rely on are placed into services. By moving your API requests to a service, you can then inject that service into the controllers that need to interact with it accordingly. However, without a deeper look at the relevant code, it's challenging to suggest the best approach.

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

How can I access an InputStream from a local XML file in a PhoneGap application?

Looking for advice on how to fetch an inputstream from a local XML file using JavaScript in my PhoneGap application. I'm new to JavaScript, so any guidance would be appreciated! ...

Issue with Webpack - npm run start and build operation not functioning?

Although I typically use create-react-app for my React projects, I decided to create one without it. However, I am encountering issues with the webpack configuration as I am not very familiar with it: This is how my package.json file looks like: { " ...

Using Angular 2 to select with default value from a separate model

Is there a way to set the default value or link to another model while utilizing NgFor on a different model? The model intended for binding is as follows: "Bookings": {"dates": [{"date":"3-10-2016","slot":"Full"}, {"date":"4-10-2016","slot":"Full"}, {"da ...

Is there a way to store my collection data in a variable and access it externally from the file?

Topic of Interest : Working with Discord.js I am seeking advice on how to save collector data in a variable and access it outside of the file, for example in index.js. I aim to create a setup command that prompts users with questions when they type -setup ...

"Tree panel items in ExtJS 4 are displaying correctly in Firefox, but are mysteriously missing from

While working on my Tree Panel project, I encountered an issue with the display of items in different browsers. The items show up correctly in Firefox but appear empty in Chromium. How is this possible? Here's the JSON data sent to the server: {"tex ...

Deleting an element in a Thinkster MEAN stack tutorial

After completing the Thinkster Mean Stack tutorial successfully, I proceeded to work on my own project. Everything was going well until I encountered a problem - deleting posts. The tutorial did not cover this aspect, and now I am stuck trying to figure ou ...

Leveraging AJAX within a RESTful API in a Node.js environment to retrieve a JSON file and dynamically parse its contents according to the specific button selected on the front-end interface

Can anyone help me with understanding the communication process between server.js (Node.js) and the front-end JavaScript file? I am trying to implement AJAX as a RESTful API in the server to retrieve a JSON file, parse it based on specific button clicks in ...

Is it possible to remove certain 'css-' class names that MUI automatically applies to its components? (Using Next JS and MUI)

After successfully migrating my create-react-app project to Next JS using the latest version (12.1.0) and following the migration guide at https://nextjs.org/docs/migrating/from-create-react-app, I encountered an unexpected issue. Despite still using MUI a ...

Looking for an Effortless Loading jQuery Image Slider or: Unleashing the Power of cross-slide

I am in need of assistance with implementing lazy loading for images in a jQuery slideshow that pulls from Flickr. Although everything is running smoothly, I would like to optimize the image loading process by only loading them on demand. Currently, I am ...

The argument supplied to the `openUri()` function should be in the form of a string, but instead, it is displaying as "undefined". Please ensure that the initial parameter provided to either `mongoose.connect()`

Encountered error: The `uri` parameter passed to `openUri()` must be a string, but it is currently set as "undefined". Please ensure that the first parameter in either `mongoose.connect()` or `mongoose.createConnection()` is a valid string. Below are the ...

Experiencing a 404 error when attempting to transmit data from controller.js to a spring controller

I encountered an issue while trying to send an ID to a Spring controller as I kept receiving a 404 error. My setup includes AngularJS, Spring, and MongoDB, where data retrieval works perfectly. The problem arises when passing the ID after clicking on the a ...

The routing functionality in Angular4 encounters issues when the `router.navigate()` method is used within the callback of a

I am currently working on implementing Google Sign In within my Angular4 app, but I have run into an unusual issue with routing after using router.navigate() in the Google Sign In callback function. To help illustrate this issue, I have created a sample d ...

What is the best way to send a prop from a parent component to its child using context in React?

Currently, I am working on fetching data in a React application. My goal is to extract an individual value from the response and pass it as a prop in a context from a parent component. The `trendingData` variable holds information retrieved from an API cal ...

Encountered an error message stating 'Unexpected Token <' while attempting to launch the node server

After adapting react-server-example (https://github.com/mhart/react-server-example), I encountered an issue with using JSX in my project. Despite making various changes like switching from Browserify to Webpack and installing babel-preset-react, I am still ...

Deploying a WebAPI to the api directory of an Angular app hosted on IIS results in 500 errors when making requests

I have been working on developing an angular application with a WebAPI 2 backend. The two components are in separate repositories, directories, and environments - the angular project is not part of a Visual Studio project and I am using WebStorm to work on ...

Executing a series of tests using Postman

Is running multiple requests in a postman script feasible? I have an endpoint: http://localhost/gadgets/{id}/buy This endpoint sets a flag in a gadget object/entry based on its id. With hundreds of gadgets, can I use a shared file of ids to create and run ...

Float over a specific line in a drawing

I am looking to develop a unique rating system using css, html, and potentially js : My goal is for the user to hover over a specific section of a circular stroke and have it fill with a particular color, all while maintaining functionality. So far, I hav ...

Issues with Autofocus in AngularJs on Firefox

Check out this straightforward directive to set autofocus: app.directive('autoFocus', function($timeout) { return { restrict: 'AC', link: function(_scope, _element) { $timeout(function(){ ...

Utilize the material-ui dialog component to accentuate the background element

In my current project, I am implementing a dialog component using V4 of material-ui. However, I am facing an issue where I want to prevent a specific element from darkening in the background. While I still want the rest of the elements to darken when the ...

Husky and lint-staged failing to run on Windows due to 'command not found' error

I'm facing issues with getting husky and lint-staged to function properly on my Windows 10 system. Here's how my setup looks like: .huskyrc.json { "hooks": { "pre-commit": "lint-staged" } } .lintstagedrc ( ...