URL hash-bang (#!/) prefix instead of simple hash (#/) in Angular 1.6

34.3k views Asked by At

My URLs on an AngularJS project have changed from localhost:3000/admin#/ to localhost:3000/admin#!/ since the last time I worked on my project...

Nothing found on the web, does someone know what this is ?

4

There are 4 answers

0
Mistalis On BEST ANSWER

It is new from AngularJS 1.6, which added a new hash prefix.

Due to aa077e8, the default hash-prefix used for $location hash-bang URLs has changed from the empty string ('') to the bang ('!'). If your application does not use HTML5 mode or is being run on browsers that do not support HTML5 mode, and you have not specified your own hash-prefix then client side URLs will now contain a ! prefix. For example, rather than mydomain.com/#/a/b/c the URL will become mydomain.com/#!/a/b/c.

Source here for more information.


If you want to remove this prefix, add this code to your config:

appModule.config(['$locationProvider', function($locationProvider) {
  $locationProvider.hashPrefix('');
}]);
0
DevDig On

In Angular 1.6.0, the default hashPrefix has been changed to !. See the related commit and the changelog entry.

1
Overdrivr On

Everyone is proposing to remove the prefix, but you could also simply add a ! to client-side URLs (if not using HTML5 mode, which you probably do if you're here).

So in your client-side files, update URLS like this:

#/foo/bar > #!/foo/bar

1
Luis Armando On

In my situation, angular 1.6 added a hashtag in this form: http://localhost:52245/#/callback#code=BD55C7FD4F129A9CAC1506E225EF9149

But I needed it in this form: http://localhost:52245/callback#code=BD55C7FD4F129A9CAC1506E225EF9149

I spent some days until find the solution. So added this configuration in the frondEnd web.config, in <system.webServer> section:

    <rewrite>
        <rules>
            <rule name="angular cli routes" stopProcessing="true">
                <match url=".*"/>
                <conditions logicalGrouping="MatchAll">
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
                    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
                    <add input="{REQUEST_URI}" pattern="^/(api)" negate="true"/>
                </conditions>
                <action type="Rewrite" url="/"/>
            </rule>
        </rules>
    </rewrite>

After that, all work ok