Execute the controller function a single time for two divs that share the same controller

I'm currently working on my project using angularjs. I have implemented two divs that share the same controller. Here is the code for my controller:

var app=angular.module('categoryPageApp', []);
app.controller('NgProducts', function($scope,UpdateService) {

   $scope.orderby=5;
   $scope.pageNumber=1;
   $scope.loadProducts=function($scope){
             //here goes ajax call
     };

 });

The two divs in question with the same controller are as follows:

<div ng-app="categoryPageApp">
<div id="cat-products" ng-controller="NgProducts">
</div>
<div id="brand-filter" ng-controller="NgProducts">
</div>
</div>

It's clear that all variables are declared twice and the update function will be called twice as well. This duplication is not desired as a single http call should suffice. Is there a way to work around this issue?

In addition, I am looking to implement a pager for my products using angular js. Any assistance or guidance on this matter would be greatly appreciated.

Answer №1

Consider storing a flag once the products are loaded by implementing the following method:

app.controller('NgProducts', function($scope,UpdateService) {
   if (!$scope.productsAreLoaded)
     $scope.orderby=5;
     $scope.pageNumber=1;
     $scope.loadProducts=function($scope){
        //perform ajax call here
        $scope.productsAreLoaded = true;
     };

});

Answer №2

follow these instructions:

<div ng-app="productCategoryApp" ng-controller="ProductController">
    <div id="products-list"></div>
    <div id="brand-filter-options"></div>
</div>

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

What is the best approach for simulating the backend in an AngularJS application?

I am in the process of testing a factory method that utilizes $resource. To carry out my test effectively, I plan to mock the real backend infrastructure, which is not yet in place. Below is an excerpt of the factory code I intend to test: app.factory( & ...

What is the best way to combine UI grid cells into a single column using AngularJS?

I am looking for a way to consolidate multiple cells into one column in AngularJs UI grid. Can this be achieved with UI-Grid? Any insights or solutions are welcome. ...

Retrieving the elapsed time in seconds from a timer function and displaying it in my

I'm looking to transfer the minutes and seconds from my template's timer to my view. I've experimented with various approaches using an ajax.post request, but it hasn't quite worked as expected. Here is a snippet of my template code: ...

JavaScript - Modifying Text within a Dropdown Menu

I'm currently working with a code that generates a dropdown menu/selector. Upon selecting an option, the main display should change accordingly. While I can't directly modify the code, I have the ability to use JQuery to alter the displayed text. ...

Sort through the array using a separate array in Vuejs

I am currently working with two arrays: { "products": [ { "name": "Jivi", "Hint": "45-60 IE/kg alle 5 Tage\n60 IE 1x/Woche\n30-40 IE 2 x/Woche", "frequency": ["1", "2", "8"] }, { "name": "Adynovi", ...

Having trouble finding the element within the form tag even after attempting to use .switchTo().defaultContent()

My HTML is structured like this: <section> <div> <form> <div>Username field</div> <div>Password field</div> <div> <div>.. <div>.. <iframe& ...

What is causing the specific sequence in which my code prints? (Exploring Javascript promises)

Can anyone help me understand promises and asynchronous programming better? I am confused because the code provided prints "1 2 0" instead of "2 1 0". Shouldn't f1 be executed only after f2 logs "2" to the console in the third block of code? const f1 ...

Getting rid of redundant elements in an array using Javascript or AngularJS

Case Study 1 A situation arises where an API I am using returns an array with duplicate values. var originalArray = [{id:1, value:23},{id:1, value:24},{id:1, value:22},{id:2, value:26},{id:3, value:26}]; //example Inquiry 1 -> How can I remove the du ...

Separate the selected option in the TEXTAREA by commas to make it easier to

Can you assist me with integrating this example? I have the following elements: When adding a textarea, I require an option to be selected and separated by a comma. For instance: Here I will select an option: Subsequently, this chosen option must be ad ...

Issue with updating nested child object reference in Redux state input value

I have a function in redux that updates an object with a specified path and value. The inputs on my page are based on the values in the object stored in state. Whenever the listingObj is modified in redux, I want the DOM to automatically refresh. This i ...

The Shopify storefront API's create cart mutation generates an HTML response when called

I recently started developing an e-commerce website using Next.js and Shopify's storefront API. I have successfully set up the connection and can list all the products from the API. However, I encountered an issue when attempting to add a product for ...

leveraging embedded jetty for developing a web-based interface

As a newcomer to web development and using embedded Jetty, I have created the source code below in Eclipse IDE. I need to programmatically start the Jetty server and cannot use the command line to do so. The web interface must be lightweight due to the li ...

Is it possible to apply the active class to the current list menu item in Mark using server-side techniques instead of client-side JS

When a menu item is clicked, I want to add the active class to that specific menu item. For example, if someone clicks on the contact page, the contact option in the menu should receive the active class. I am currently working with NodeJS and Koa. Below is ...

Understanding the process of retrieving a data value from HTML in an AngularJS directive

I'm a beginner with Angular and I'm trying to pass some data to my angular directive from the template. <div class="col-md-6" approver-picker="partner.approverPlan.data" data-pickerType="PLAN"></div> I h ...

PHP strtotime is adding an extra hour to my time

Within my database table, I have a datetime stored as '2014-08-05 15:12:00'. After using strtotime() to convert this date to milliseconds, on the client side I create a new Date object as follows: var date = new Date(date_in_milliseconds) Ho ...

Using Vue to display a Div inside a TD element of a table does not result in a reactive behavior

I am encountering an issue with a table where the last column contains a div with three options (View, Edit, and Delete). This submenu is initially hidden, but when clicking on the options button in the last column of the table, the array used to control i ...

Learn how to efficiently process a data queue with AngularJS using Promises

Within my AngularJS application, I have a Service and multiple controllers running simultaneously. The app receives data updates from the server in JSON format through the Angular Service. To manage the vast amount of data, I need to queue it within the se ...

Navigating through cors in next.js

Currently, I have set up my front end using Netlify and my backend using Heroku with Next.js For the fetch request on the front end, here is an example: fetch(`https://backendname.herokuapp.com/data`, { method: 'POST', headers: { & ...

Do you think it's wise to utilize React.Context for injecting UI components?

I have a plan to create my own specialized react component library. These components will mainly focus on implementing specific logic rather than being full-fledged UI components. One key requirement is that users should have the flexibility to define a se ...

Locating an element outside of a class using jQuery

I am trying to target the initial element that matches my criteria OUTSIDE of my container. Even though I can easily find it when it's inside, I struggle when it's outside. My goal is to specifically select the very first element it discover ...