Starting up an AngularJS controller with varying sets of data

In my Angular app, I am utilizing $routeProvider for routing purposes. There are 2 routes where I am using the same HTML template and controller.

        when('/products, {
            templateUrl: 'views/products.html',
            controller: 'ProductListCtrl'
        }).
        when('/myProducts', {
            templateUrl: 'views/products.html',
            controller: 'ProductListCtrl'
        }).

The only variation lies in the data that needs to be displayed. Specifically, for the products path, I want an AJAX request to myserver:8080/products, whereas for the myProducts path, I need to retrieve data from an AJAX request to myserver:8080/products/my.

Currently, I am using the $location service to determine the current page (products or myProducts) and load the appropriate information.

I am curious if there is a more elegant solution to achieve this. Is it possible to utilize the resolve method of $routeProvider?

Answer ā„–1

To effectively reuse a controller name in the present scenario, utilizing resolve with $routeParams is highly recommended.

You can make modifications to your code as shown below

when('/products, {
                templateUrl: 'views/products.html',
                controller: 'ProductListCtrl',
    resolve: {
            product: function($http) {
              return $http.get('/products')
            },
              needToShowFilter:function($q){
               var showfilter = $q.defer();
                   showfilter.resolve(false);
               return showfilter.promise
            }
          }
            }).
            when('/myProducts', {
                templateUrl: 'views/products.html',
                controller: 'ProductListCtrl',
    resolve: {
            product: function($http) {
              return $http.get('/products/my')
            },
            needToShowFilter:function($q){
               var showfilter = $q.defer();
                   showfilter.resolve(true);
               return showfilter.promise
            }
          }
            }).

Afterwards, you can inject the product into the controller code.

Answer ā„–2

Consider including $route in your controller and then output

$route.current

to inspect the content, this should help you retrieve the information

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

Sending numerous HTML forms using a sole submit button through AJAX

On my website, I have a page named publish.php where users can input details for each image they upload. Each image has its own form, but only one form is displayed at a time in a tabbed layout when the user clicks on the corresponding thumbnail. I want to ...

Requesting data download via AJAX for a CSV file

I have a unique feature on my website where users can customize and download a summary page based on specific elements they select to include in the final CSV file. To streamline the process, I want to display the summary page to the user and initiate the ...

What is the best way to extract data using Angular from the backend and then transfer it to the frontend using Node.js?

Recently, I decided to make a change in my node.js application. Previously, I was using the EJS template engine but now I want to switch to Angular. To do this, I have already installed Angular and it is working well. However, I am facing an issue while t ...

What steps should I take to resolve npm start issues within my Node.js application?

Upon completing the npm install, I attempted to run npm start for my project. Unfortunately, an error was displayed. What steps can be taken to resolve this issue?view image of the error here ...

Prevent button from functioning after loading specific div using Jquery

Encountering an issue with the div after a successful ajax call and loading. See below for the code snippet: <script> $('.delete').click(function(e){ e.preventDefault(); var id = $(this).attr('id'); if (confirm(&a ...

Experiencing difficulties when trying to input text into a React text field

For a university assignment, I am using the create-react-app to build a website. I am facing issues with the input text field as I cannot type into it when clicked. There are no error messages indicating what could be wrong. The objective is for users to ...

Accessing variables in subfunctions proves challenging for Node.js

Here is a function I've been working on to calculate the total price for a checkout process. However, when I console.log() the variable before it is returned, I get 0. But if I log the variable inside the findOne() function, I get the correct value. ...

What would be your approach to incorporating code highlighting while scrolling?

Is there a library available or what would be the best way to create a blog page with code highlighting similar to Stripe's documentation? I want the code on the page to highlight as the user scrolls, just like in the Stripe documentation. Any recomme ...

Submitting Forms using AJAX in Razor with MVC3

When utilizing The MVC3 Helper to create my Ajax form, it appears as follows: @using (Ajax.BeginForm("Attended", "Lesson", new AjaxOptions { HttpMethod = "GET", InsertionMode = InsertionMode.InsertAfter ...

Guide on incorporating dxTreeView with AngularJS

Hello there, I am trying to utilize the dx-tree-view component. In my Home.html file, I have included the following HTML code: <div dx-tree-view="treeViewOptions"></div> And in my HomeController.js: $scope.treeViewOptions = { binding ...

EINVAL: the argument provided is invalid for reading

Iā€™m currently using node v11.5 and npm 6.4.1 on a flash drive labeled E, running on Windows 7. My goal is to install the latest version of the Netlify CLI. I followed the steps outlined at , and executed the following command: $ npm install netlify-cli ...

Struggling to dynamically update the URL within a "a href" tag, JavaScript is failing to reflect the changes in the output

Currently, I am attempting to use JavaScript to replace the URLs in the HTML. However, I seem to be encountering an issue where the new URLs do not apply when clicking on the images, even though testing with "alert" shows that the values are correct. Any a ...

Leveraging String Interpolation for Retrieving Array Values in Angular2 Application

In my Angular2 application, I have implemented a functionality where I loop through a list of users and display an icon to represent each user. Now, I want to enhance the user experience by using material2's tooltip (mdTooltip) feature to display the ...

ASP.NET Core Ajax response is empty

function addNewTeam() { var teamName = $("teamField").val(); $.ajax({ type: "POST", url: 'AddNewTeam', contentType: "application/json;", data: { team: "HELLO H ...

What is the process for generating an HTML document from start to finish with the 'html-element' node module?

Here is an example that demonstrates a flawed method: const HTML = require('html-element'); const doc = `<body> </body>`; const page = HTML.document.createElement(doc) page.appendChild('<div>1</div>') page.append ...

Can a single static variable be shared among all connections on the server?

I am currently working on a turn-based Chinese checkers game. I have added an onload function in the body that sends an ajax request to the server to obtain the player number for the connection. However, I am encountering an issue where the response always ...

Submitting a form using an anchor tag in Angular 8: A step-by-step guide

I have a question about how to submit form data using hidden input fields when a user clicks on an <a> tag. <form action="/submit/form/link"> <input type="hidden" [attr.value]="orderNumber.id" /> <input type="hidden" [attr.value]= ...

I am interested in running JavaScript code on a page that has been loaded through AJAX

I am struggling to execute the Javascript loaded within an ajax page. Here is the link to my loaded ajax page: https://jsfiddle.net/4dsbry7j/ JS : var xmlhttp = new XMLHttpRequest(); var url = "http://localhost/ajax/find2.php"; xmlhttp.onreadystatechang ...

Utilize ASP.net to efficiently store and retrieve events in a SQL database

If you have an SQL database table named Calendar, here are the tasks you may want to accomplish: You can achieve the following using Ajax: Retrieve events from the database and pass a List to your website. Save events back to the database. Note that ev ...

Having trouble setting the locale in momentJS?

I have successfully integrated momentJS with Ionic/Angular using bower, and it is working well. However, when I try to change the locale to 'fr' or 'da', the output still remains in English even though I have the necessary locale files ...