admin管理员组文章数量:1336576
I am currently using AngularJS with UI-Router for state management and express on the back-end.
I currently have hash bang mode enabled and have been trying to find a solution that allows you type URL's without the hash bang manually in the address bar.
So for example instead of typing:
www.mysite/#!/page1
I want to able to type
www.mysite/page1
I have tried by enable html5Mode:
$locationProvider.html5Mode(true)
.hashPrefix('!');
But it still doesn't work. With the above change, clicking any URL's will re-write and change location from '#!/page1' to '/page1'. Which is great.
However manually entering a URL with '/page1' does not work and gives an invalid path error. If I type it with hash bang included i.e. '#!/page1' then it works and also re-writes the URL as the '/page1' after page load.
So what am I doing wrong here?
My states look like this:
state('login', {
url: '/login',
templateUrl: 'login.html'
}).
state('parent', {
url: '/hello',
abstract: true,
template: '<ui-view/>'
}).
state('parent.home', {
url: '',
controller: 'SomeCtrl',
templateUrl: 'page.html'
}).
state('parent.file', {
url: '/bob',
controller: 'SomeCtrl2',
templateUrl: 'file.html'
})
I am currently using AngularJS with UI-Router for state management and express on the back-end.
I currently have hash bang mode enabled and have been trying to find a solution that allows you type URL's without the hash bang manually in the address bar.
So for example instead of typing:
www.mysite./#!/page1
I want to able to type
www.mysite./page1
I have tried by enable html5Mode:
$locationProvider.html5Mode(true)
.hashPrefix('!');
But it still doesn't work. With the above change, clicking any URL's will re-write and change location from '#!/page1' to '/page1'. Which is great.
However manually entering a URL with '/page1' does not work and gives an invalid path error. If I type it with hash bang included i.e. '#!/page1' then it works and also re-writes the URL as the '/page1' after page load.
So what am I doing wrong here?
My states look like this:
state('login', {
url: '/login',
templateUrl: 'login.html'
}).
state('parent', {
url: '/hello',
abstract: true,
template: '<ui-view/>'
}).
state('parent.home', {
url: '',
controller: 'SomeCtrl',
templateUrl: 'page.html'
}).
state('parent.file', {
url: '/bob',
controller: 'SomeCtrl2',
templateUrl: 'file.html'
})
Share
Improve this question
edited Oct 9, 2014 at 19:44
mscdex
107k15 gold badges201 silver badges159 bronze badges
asked Oct 9, 2014 at 19:36
iQ.iQ.
3,9536 gold badges40 silver badges60 bronze badges
2
-
Have you tried just dropping the prefix? Or, are you doing something that needs it? I just have
$locationProvider.html5Mode(true);
. – Owen Commented Oct 9, 2014 at 19:39 - yes I did, no luck so it sees as livepo suggests, it could be server side, I'm going to try it out. – iQ. Commented Oct 9, 2014 at 19:58
3 Answers
Reset to default 4UPDATE: Your code looks fine to me, i'm going off of experience and think this is a server issue.
There are a few ways you can handle this but as far as I know they are only server side.
using AngularJS html5mode with nodeJS and Express
How do I configure IIS for URL Rewriting an AngularJS application in HTML5 mode?
The reason why you cannot go directly to the route - mywebsite./page1 is because the angular app itself (which resides on the index.html page) needs to intercept/direct you to mypage. This is why you see that the page itself is never reloaded, the views are just changing in your app. You can also test this out by refreshing the page too.
You will need implement something server side, that takes your request (IE: /page1) and route all requests to index.html where Angular will take care of the rest.
Additionally: When you use #/page1, you are still requesting your index page, angular recognizes the # and routes accordingly
Edit: Additional Information
This because /hello
is a 404 on the server. I have a catchall at the bottom of my routes:
app.route('/*')
.get(function(req, res) {
res.sendfile(app.get('appPath') + '/index.html');
});
Use a slash at the end of url:/path/
Example:
state('parent', {
url: '/hello/',
abstract: true,
template: '<ui-view/>'
})
本文标签: javascriptAngularJSUIRouterManually Type URLs in HTML5 Mode without HashBangStack Overflow
版权声明:本文标题:javascript - AngularJS + UI-Router - Manually Type URLs in HTML5 Mode without HashBang - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742410555a2469699.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论