Unexpected request URL error occurs when specifying a template in an AngularJS route

When setting up my routes in AngularJS, I have the following configuration:

discountLinkApp.config(['$routeProvider', '$locationProvider',
  function($routeProvider, $locationProvider) {
    $routeProvider.
      when('/discount-links', {
        templateUrl: 'js/partials/discount-links.html',
        controller: 'discountLinkController'
      }).
      when('/register/:tag', {
        templateUrl: 'js/partials/register.html',
        controller: 'paymentPageController'
      }).
      otherwise({
        redirectTo: '/discount-links'
      });

    //Removes the # from the URL
    $locationProvider.html5Mode(true);
  }]);

The structure of my root directory looks like this:

├── index.php
├── js
│   ├── app.js
│   ├── controllers
│   │   └── mainCtrl.js
│   ├── partials
│   │   ├── discount-links.html
│   │   └── register.html
│   └── services
│       └── discountLinkService.js
└── views
    └── index.php

For the /discount-links route, AngularJS correctly requests:

/js/partials/discount-links.html

However, for the '/register/:tag' route, it seems to be looking for

/register/js/partials/register.html

Notice the additional /register/ segment which does not exist, resulting in a 404 error.

I am trying to understand why this discrepancy is occurring. The only difference between the two routes is the :tag.

To solve this issue, I can add ../ to the templateUrl so it becomes ../js/partials/register.html, but this feels inconsistent to me.

Does anyone have any insights or ideas on how to resolve this?

Answer №1

To make it work on every path, just switch to using absolute paths in the templateUrl property. Instead of:

js/partials/discount-links.html

simply change it to:

/js/partials/discount-links.html

That way, your code will function properly regardless of the path.

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

Changing the application's state from within a child component using React and Flux

UPDATE It seems that my initial approach was completely off base. According to the accepted answer, a good starting point is the TodoMVC app built with React + Flux and available on GitHub. I am currently working on a small React + Flux application for ed ...

Express.js application experiencing technical difficulties

When attempting to create a simple Express application with the file called serv.js, I encountered an error. Here is the code snippet I used: var express = require('express'), app = express(); app.listen(3000, function() { c ...

Complete a submission using an anchor (<a>) tag containing a specified value in ASP.Net MVC by utilizing Html.BeginForm

I am currently using Html.BeginFrom to generate a form tag and submit a request for external login providers. The HttpPost action in Account Controller // // POST: /Account/ExternalLogin [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public Acti ...

Unable to show additional tooltip information on the price history chart

In an attempt to enhance the tooltip of a price-history chart (stockChart), I am trying to display extra data. Here is a jsfiddle example: http://jsfiddle.net/z10dLcj8/1/ $(function(){ var priceHistoryObjArray = [ [1379883600000,47.19,'extra ...

Issue with facebook button in reactJS where onClick() event is not being triggered

I'm facing an issue where my onClick handler is not working with the Facebook button from https://developers.facebook.com/docs/facebook-login/web/login-button/. However, it works fine with a regular div element. Any thoughts on why the onClick event ...

Retrieving fresh data with Ajax from Django 1.9 upon button click

I'm currently working on a code to display the status of AWS instances (servers) in a browser when users want to check the status. In some cases, it may take some time for the servers to start up, so I would like the status value to be refreshed (run ...

Give Jquery a quick breather

My goal is to have my program pause for 3 seconds before continuing with the rest of the code. I've been researching online, but all I can find are methods that delay specific lines of code, which is not what I need. What I would like to achieve look ...

Does the reduce method work by default like this?

const product_list=[{id:1},{id:2}] const temp_products=[{id:1,quantity:10},{id:2,quantity:20}] product_list.map(prod=>{ console.log(temp_products.reduce((init,curr_prod)=>{ return curr_prod.id == prod.id ? curr_prod.quantity : null })) ...

The CSS I've written isn't functioning properly within a div that was generated using

I have been trying to populate a div with data using JSON, but I am facing an issue where my div, which is set with JavaScript for each JSON element, does not seem to be working correctly. The classes "lulu" and "lora" are not being recognized. Apologies f ...

UI Datepicker experiencing issues when setting a minimum date

Currently, I am utilizing AngularJS with a datepicker setup as follows: <div ng-controller="MyController as vm"> <div class="small-8 columns right-align"> <input id="dateFrom" type="text" name="FromDate" ...

AngularJS framework may encounter an issue where changes in $scope data do not reflect in the view

I have noticed that when I reload the data using my function, the view does not change. After some research, I found that adding $scope.$apply() should solve this issue. However, I am encountering an error when trying to implement this solution. https://d ...

React: Applying the active class to mapped elements

I have a component that iterates over an array to generate 10 navigation items. I want to implement an onClick method that will add an active class to the clicked item. Are there any best practices using Hooks or the newer React patterns for achieving this ...

Remove an element from an array based on the index provided by the front end

Currently, I am attempting to eliminate a specific item from a PHP array stored in a JSON file. Here is an example of the JSON file structure: { "1.49514754373E+12": { "description": "I don't like it", "fileNames": [ " ...

Exploring the nuances between Ruby on Rails and the responses from json and JavaScript ajax

I am interested in learning the most effective method for handling an ajax request. Would it be better to send json data and parse it on the client side (for instance using pure), or should I generate javascript at the server side and send back the respo ...

Unable to find the designated ./uploads directory while attempting to upload a file in Express JS

I'm currently working on a project with the following structure: -app -uploads -client - dropzone.js -server.js My goal is to upload a file using dropzone js, Express JS, and React JS. However, whenever I try to drop a file in ...

Error: Unable to access the 'offsetTop' property of null

I attempted to implement a scroll effect in my project. The goal was for users to be taken to a specific section when they clicked on an option in the navbar. However, I encountered an error during implementation. Below is the code snippet where the error ...

p5 - Syntax error: anticipated semicolon but received hour

Here is the code snippet in question: function setup() { createCanvas(500, 500); angleMode(DEGREES); } function draw() { background(0); translate(width/2, height/2); let hour = hour(); } There is an error on let hour = hour(); indicat ...

What could be causing the error when attempting to release this Node-MySQL pool?

My first attempt at utilizing connection pooling is with Node.js. I crafted a database connection module in Node.js to establish a single connection, which is then referenced within a pooling function for later use when passed to different modules. /* D ...

when within a 'for in' loop

Querying Object Equivalence: I find myself struggling to comprehend the flow of my code and how I can rectify it. Within the areEqual function, I am comparing the "value" property of two objects. If they match -> isEqual=true -> continue with the ...

What is the method for accessing appendTo() in the Document Object Model (

I added a duplicated element $canvas to the body in the DOM with this piece of code $('.' + $canvas).clone().appendTo('body'); Now, I want to be able to use it like this $('ul,.map').mousemove(function (e) { $(& ...