Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS: route provider changes "/" into %2F [duplicate]

I am building an AngularJS appplication and I have problems with the URL when building the second view:

My appContact.js looks like this:

(function () {

    "use strict";

    var app = angular.module("appContacts", ["simpleControls", "ngRoute"])
        .config(function ($routeProvider, $locationProvider) {

            $routeProvider.when("/", {
                controller: "contactsController",
                controllerAs: "vm",
                templateUrl: "/views/contactsView.html"
            });

            $routeProvider.when("/details/:firstName", {
                controller: "contactsDetailsController",
                controllerAs: "vm",
                templateUrl: "/views/detailsView.html"
            });

            $routeProvider.otherwise({ redirectTo: "/"});

            $locationProvider.html5Mode({
                enabled: true,
                requireBase: false
            });

        });
})();

In my HTML I have this link:

<a asp-controller="Contact" asp-action="Details" ng-href="#/details/{{contact.firstName}}" >{{ contact.firstName}}</a>

When I hover in the Browser I have the correct proposed link like:

 **http://localhost:8000/#/details/Matthew**

But when I click the link to navigate to the new page the URL changes to

 **http://localhost:8000/#%2Fdetails%2FMatthew** 

Changing the "/" by %2 make the app fail and a blank page is shown.

Do you know why is this and how to correct this issue?

I have already read similar posts her but none of them seems to work since I have not access to the encoded URL before it reaches the Browser and the error is there.

Thanks

Rafael

like image 841
Rafael Munoz Avatar asked Aug 12 '16 08:08

Rafael Munoz


People also ask

How do we set a default route in $routeProvider?

Creating a Default Route in AngularJS The below syntax just simply means to redirect to a different page if any of the existing routes don't match. otherwise ({ redirectTo: 'page' }); Let's use the same example above and add a default route to our $routeProvider service. function($routeProvider){ $routeProvider.

Which of the following statement is true in the case of Route provider?

Answer: D is the correct answer. 30) Which of the following statement is true in the case of $routeProvider? It is a service.

What is the $routeProvider in AngularJS used for?

The $routeProvider is creates the $route service. By configuring the $routeProvider before the $route service we can set routes in HTML templates which will be displayed. The $routeProvider is configured with the help of calls to the when() and otherwise() functions.

Which of the following makes route path case sensitive in AngularJS?

That's because the routes that are configured using UI-Router are case sensitive. To make route insensitive, all you have to do is to inject $urlMatcherFactoryProvider service into the config() function and call caseInsensitive(true) function.


1 Answers

May this one will help you.

include $locationProvider.hashPrefix(''); in your config.

           Example.
         <div>
            <a href ="#/table">table</a>
            <a href ="#/addPage">addForm</a>
        </div>

        app.config(function($routeProvider, $locationProvider) {
            $locationProvider.hashPrefix('');
         $routeProvider.when("/addPage", {

                           templateUrl :"partials/add.html",
                           controller : "ngRepeatTableCtrl"

                         })
                      .when("/tablePage", {

                           templateUrl :"partials/table.html",
                           controller : "ngRepeatTableCtrl"
                     })
                      .otherwise({
                             templateUrl :"partials/table.html",
                             controller : "ngRepeatTableCtrl"

                        });


    });
like image 90
Himanshu Shekhar Avatar answered Oct 01 '22 03:10

Himanshu Shekhar