Tips for preserving the URL of a 404 page in AngularJS

We are currently developing an angularjs application where users can publish their profiles as resumes.

For example, a valid URL for a published profile would be:

www.page.com/public/johnsmith

However, if the URL is something like:

www.page.com/public/johnsmith2345

which is not a public profile, I want to display a 404 page. The challenge is that due to angular's routing, I need to redirect to the 404 page, resulting in the URL being rewritten as:

www.page.com/404.

What is the correct way to maintain the original URL and ensure it works in IE9?

Answer №1

One approach to handling this situation is by implementing logic in your controller or view. For example:

$routeProvider.when('/public/:user', 
    {
        templateUrl: 'profile.html',
        controller: 'profileCtrl',
        resolve: {
            userProfile: {
                //Fetch the user's profile and return null if not found
            }
        }
    }
);

var profileCtrl = function($scope, userProfile) {
    $scope.userProfile = userProfile;
}

In your profile.html:

<div ng-if="!userProfile">
    <div ng-include="404page.html"></div>
</div>
<div ng-if="userProfile">
    ... Display the user's profile here
</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

Utilizing Ajax to send a parameter to a separate PHP script

I have a dilemma that I need help with. Currently, I have a table displaying data using PHP and SQL in the following format: What I want to achieve is to be able to click a button, retrieve the ID value, and based on that, execute another SQL query to dis ...

Executing a function from a dynamically generated element in AngularJS

I'm currently working on a custom query builder in AngularJS by modifying the angular-query-builder plugin. Here is a snippet of the code structure relevant to the context: base.html <query-builder group="filter.group"></query-builder> ...

What is the best way to extract value from a specific link using JavaScript?

I have a PHP script that retrieves data from a database: <?php while($trackResultRow = mysqli_fetch_assoc($trackResult)){?> <a onclick="remove()"><span class="glyphicon glyphicon-thumbs-down pull-right"></span></a> < ...

What are the best ways to enhance performance when making multiple require() calls?

I am in the process of developing a bot for my company's Mattermost server (similar to a Discord bot). Each command for the bot is housed in its own file, making it easier to manage and maintain. When a user wants to execute a command, they must send ...

Whenever the page is refreshed, the props in my application are dynamically updated thanks to the getStaticProps function I utilize

Currently, I am in the process of learning nextjs as a beginner. Through the utilization of the getStaticProps function, I have come to understand that data fetching occurs only once at build time and the values remain static thereafter. Despite this, I ...

Ways to customize the default countdown timer

I came across this amazing project at https://codepen.io/mattlitzinger/pen/ysowF. While the project is wonderful, I am looking to make some modifications to the code, specifically targeting a specific date. Here is the JavaScript code snippet: var tar ...

Transferring the value of a TextBox to another TextBox in ASP.NET across multiple rows

I have around 10 rows where I need to duplicate the value of one TextBox into another. A specific ID can be assigned to the first TextBox to achieve this, but I am seeking a more general function that can copy TextBox values in each row in ASP.NET. This i ...

Incorporate a CSS-styled Hyperlink Button into a Jquery Loopedslider

After spending the last 2 hours trying to figure out what seems to be a simple solution, I am determined to add a hyperlink button to each image/div in the jquery Loopedslider using CSS. Although I have used loopedslider multiple times before, I have neve ...

RectAreaLight in Three js does not produce any light reflection when used with MeshPhongMaterial due to lack of support for OES_texture_half

After trying to incorporate a RectAreaLight into my three.js scene where I have objects with MeshPhongMaterial, I noticed that there is no light reflection on the objects. A useful example can be found here: Link If you open the developer tools, you can s ...

Is there a way to trigger a function in an AngularJS controller from a Backbone controller?

I've been working on an application that was originally developed using backbone and jQuery, but we had to incorporate new modules built with angular to meet client requirements. Routing in the application is handled by backbone route, and we have suc ...

JQuery HTML form validation continues to send emails even when there are blank entries

I've set up an HTML form with three required fields, and I'm trying to prevent the form from submitting an AJAX call if those fields are left empty. $("#contact").submit(function(e){ e.preventDefault(); var ajaxurl = '<?php echo ...

How to send Multipart form data with a string payload

Many suggestions in regards to this issue recommend utilizing some form of FormData within nodejs for building a multipart form. However, I am seeking to achieve the same result without relying on the FormData library. Instead, I aim to use only request h ...

Tips for displaying a temporary image placeholder while the actual image loads

One innovative approach is to display a low-resolution version of an image as a placeholder while the high-resolution image loads. This can be achieved using an img tag with specific attributes. <img lowres="http://localhost/low-res-image.jpg" ng-src=" ...

What is the best way to send an array of grouped data to a table

Here's how I organized the array: { "2023-10-01": [ { "emp_id": 1, "name": "Aruna", "code": "DO", "date": "2023-10-01" }, { &qu ...

What are some effective solutions for resolving design problems in Isotope?

When I append the ajax coding, the data is coming fine. However, when I append this style="position: absolute; left: 468px; top: 0px;" css to the new appended data, it is not working. How can I achieve this? Below is my ajax code. Please provide suggestion ...

Ways to showcase the resulting answer using vue.js?

My current query is regarding the display of error messages. How can I show the error message "passwords do not match" {"errors": {"password": ["Passwords donot match"]}, "status": false, "msg": "Validation erro", "error-type": 0} In my code snippet, I h ...

Ways to implement CSS with javascript

I'm using PHP to retrieve data from my database and then leveraging Javascript to enable users to add it. However, I am facing challenges in making a div change its background color and apply borders. <div class="displayedItems"></div> &l ...

Struggling to make the upvoting feature function properly in the Thinkster MEAN Stack Tutorial

While following the MEAN Stack tutorial on this website, I decided to modify my code to utilize Controller as instead of using $scope as demonstrated in their examples. I am encountering an issue with the upvote functionality. Whenever I click to upvote a ...

Retrieving information from the mainframe

Is there a way to retrieve data from the server using a factory and then return an object that includes this data? The data I need is stored in the directory data/es/20130709.json relative to index.html. This data (20130709.json) is structured like this: ...

Error in Node.js: Trying to access property 'get' of an undefined object

Although I have some knowledge of JavaScript, I am new to Node.js. Despite it being a common issue, I am having trouble identifying the source of the error because my debugging skills in Node.js are lacking. Here is my app.js: var index = require('. ...