3

I know this is a bad question, and I have no code but I was hoping someone can possible shed some light on this.

I have an Angular site.

The site loads sales data for different products. The products names are shown in the URL

The site works fine from links in the site to the different products.

If I update the URL directly with the product name and press enter and site does not reload. I have to select the whoile URL and press enter again for the page to reload.

Can anyone think why this might be happening?

4
  • can you give me a example so i can tell you Commented Dec 21, 2017 at 11:26
  • As per me the in the Url it should get A proper Name with its Case URL in Angular are case sensitive so . Commented Dec 21, 2017 at 11:27
  • It's changing a parameter in the url so I don't know if it will help. It could be like my-site.co.uk/products/100 and if I change it to my-site.co.uk/products/101 and press enter it won't load the page until I select the url again and press enter Commented Dec 21, 2017 at 11:46
  • Subscribe to ActivatedRouter angular.io/guide/… Commented Dec 21, 2017 at 11:48

1 Answer 1

1

You need a way to tell the server that the URLs you are entering are valid Angular routes. And hence you need to map these URLs to index.html.

Since I use Java/Spring at the back-end. Here is how I do it. This solves the problem:

@Controller
public class Routing {

    @RequestMapping({"/resources/**"}) 
    public String api(final HttpServletRequest request){
        String path = (String) request.getAttribute(
                HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
        return path;
    }

    @RequestMapping({ "", "/login", "/products/**" })
    public String gui() {
        return "forward:/index.html";
    }
}

Here the first method just returns the paths that starts with "/resources/**", because under this path, all my REST APIs are available. The second method forwards to index.html for the Angular routes.

Note that forwarding to index.html does not change the URL on the browser address bar, but it triggers Angular to take control.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.