Navigating in AngularJS with various URL parameters

Within my application, I am in need of using routes that require multiple attributes from the URL to be passed into PHP.

The current setup that is functioning correctly is as follows:

.when('/jobs/:type', {
        templateUrl: function(attrs){ return '/pages/jobs.php?_type=' + attrs.type; },
        controller  : 'jobsController'
    })

This allows me to access _type using a $_GET request in jobs.php.

I am attempting to modify it so that I can include multiple attrs in my template Url. Here's an example of what I have tried:

.when('/page/:data1/:data2', {
        templateUrl: function(attrs){ return '/pages/page.php?data1=' + attrs.data1 + '&data2=' + attrs.data2; },
        controller  : 'mainController'
    })

However, this adjustment is causing issues with my application. I believe there must be a simple solution to this problem, but due to my limited experience with Javascript and Angular, I cannot seem to figure it out.

Any help would be greatly appreciated. Thank you.

Answer №1

It is not recommended to assign a PHP file as a templateUrl in AngularJS, as it may limit the use of AngularJS features within the PHP file. A better approach would be to call the PHP file through a service and manipulate the data using Angular on the front-end. One way to achieve this could be:

ROUTER

.when('/page/:data1/:data2', {
    templateUrl: views/page.html,
    controller  : 'mainController'
})

CONTROLLER

app.module('myApp').controller('mainController', function(pageService, $scope){
  var init = function(){
  var data1 = '1234';
  var data2 = '4567';
 pageService.getPageData(data1, data2)
            .success(function(data){
                $scope.data = data;
             //
             })
}
init();
})

SERVICE

app.module('myApp').controller('pageService', function($http){
    var factory = {};

    factory.getPageDetails(data1, data2){
        return $http({
            url: '/pages/page.php?data1=' + data1 + '&data2=' + data2;,
            method: 'GET'
        });
    }
return factory;
});

Answer №2

I made a minor error by accidentally referring to the incorrect file.

.when('/page/:data1/:data2', {
    templateUrl: function(attributes){ return '/pages/page.php?data1=' + attributes.data1 + '&data2=' + attributes.data2; },
    controller  : 'mainController'
})

This solution is effective.

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

Encountering problems with createMediaElementSource in TypeScript/React when using the Web Audio API

Currently, I am following a Web Audio API tutorial from MDN, but with a twist - I am using TypeScript and React instead of vanilla JavaScript. In my React project created with create-react-app, I am utilizing the useRef hook to reference the audio element ...

Creating an infinite loop using Jquery's append and setTimeout functions

I'm having trouble displaying my JSON data in a table and refreshing it periodically to check for new entries. Unfortunately, I seem to have gotten stuck in an infinite loop where the setTimeOut function keeps adding old entries. Can anyone help me tr ...

I am unable to generate a vite application within WSL

My system is running node version 10.19.0 and npm version 6.14.4. Whenever I try to run create-vite@latest client, I encounter the following error: npx: installed 1 in 0.79s /home/victor/.npm/_npx/86414/lib/node_modules/create-vite/index.js:3 import &apos ...

Wait until the user submits the form before activating Angular validations, and do not display any validation messages if the user deletes text from a text box

I am looking to implement angular validations that are only triggered after the user clicks on submit. I want the validations to remain hidden if the user removes text from a textbox after submitting it. Here is what I need: - Validations should not be dis ...

Deleting an item from a Vue.js list

I'm currently working on a project to develop a basic webpage that allows users to add or remove textboxes using vue.js. new Vue({ el: "#app", data() { return { list: [] }; }, methods: { deleteFromList(index) { this ...

"Unable to locate the specified file or directory" error message pops up while attempting to save a file

Currently, I am in the process of generating a JSON file using my website with intentions to deploy it later. Below is the code snippet that I have implemented: saveFile = (i, data) => { var filename = `${i}_may.json`; var folder_list = ["desktop", ...

What is the best way to identify the property of an object that holds a specific value?

Searching through an object array in JavaScript to find a specific value and identify the property containing that value is my current task. Here's an example: Object Array: var objArray = [{ "ID": 1, "Name": "Kathy", "Position": "Progra ...

Encountering a 404 error while accessing my meticulously crafted express server

After ensuring that the server is correctly set up and without any errors related to imports or missing libraries, I utilized cors for development purposes. A 404 error persisted even after attempting to comment out the bodyparser. https://i.stack.imgur.c ...

Can a custom directive with dynamic behavior be crafted in AngularJS?

Allow me to begin by explaining my intentions with broken-pseudocode: // controllers.js angular.module('myApp.controllers', []). controller('MyCtrl1', [function() { var template = '<div my-directive></div>' ...

Establish a table containing rows derived from an object

I am currently dealing with a challenge in creating a table that contains an array of nested objects. The array I have follows this schema: array = [ { id: 'Column1', rows: { row1: 'A', row2 ...

How to retrieve the chosen option from a drop-down menu created within a loop in a React application

I am seeking guidance on the best way to create multiple drop-down menus (<select>) using a loop, so that I can retrieve their selected values using a button. I have attempted to replicate what is currently in my code, hoping this information is suff ...

Discovering the function responsible for styling a DOM elementDiscovering how to identify the

I have a Prestashop 1.6 store and I've noticed that when I scroll down, my main menu becomes fixed at the top of the page. However, when I scroll back up, it reverts to its default position in the header. The issue is that the height of the menu is se ...

The silent button malfunctioning - Ionic

Within the html file: <video preload="metadata" muted="true" controls playsinline webkit-playsinline loop> Despite the "muted" tag, the video is playing with sound in both the browser and on the device. After referencing the video and setting the ...

Adjusting the filter location in React-admin

This is the common method of implementing filters in react-admin https://i.stack.imgur.com/M8yq7.png However, in my particular scenario, I require the filters to be inside each column heading. For example: https://i.stack.imgur.com/GU2Pz.png The filter ...

Learn the process of dynamically loading scripts and their functions in Angular

At first, I had the following code statically placed in Index.html and it was functional. <script src="/le.min.js"></script> <script> LE.init({ token: 'token', region: 'x' }); </script> ...

Employing require.js, one can integrate a distinctive form of non-concatenated dat.gui source. This allows for the seamless

Excuse the SEO-friendly title, but I want to ensure that everyone can access the issue I'm currently working on. For those interested in customizing the appearance of dat.gui, you will need to download the source and include it using require.js follow ...

Prevent $.ajax with jQuery when a button is clicked

Is there a way to interrupt the $.ajax() function execution by clicking on this button: <button class="stop">Stop</button> Is there a specific function that can cause the $.ajax() call to stop? Note: The $.ajax script is within a function, l ...

Enhance DataTables functionality by including the ability to select which script to execute

Currently, I have a DataTables displayed with the provided code, utilizing server-side processing which is functioning properly. I am interested in implementing a dropdown menu above the table that allows users to select from options such as: Product Gr ...

I'm struggling to solve a straightforward jQuery sliding issue

I am struggling to make text slide from right to left on my website. I want the text to appear only when the page loads or refreshes, and then slide off when a link is clicked, revealing new text. Can anyone help me figure this out? http://jsfiddle.net/XA ...

"Navigate back to a previous page in Vue Router without having to

I am currently exploring the option of creating a back button in my Vue.js application using vue-router that mimics the behavior of the browser's native back button. The challenge I'm facing is that when using history mode for the router and tryi ...