I want to reload the page with different html page using angularjs $window, the documation says:
Page reload navigation
The $location service allows you to change only the URL; it does not allow you to reload the page. When you need to change the URL and reload the page or navigate to a different page, please use a lower level API, $window.location.href.
So I set the loaction to the page $window.location.href = '/transaction';
and the matching route on config block:
app.config(function($routeProvider) {
$routeProvider
.when('/transaction', {
templateUrl : 'JavaScript/directives/modelHTML/Transaction.html',
controller : 'endProcessModelController'
});
});
But instrad of Transaction.html page I get Error 404: SRVE0190E: File not found: /transaction with the route mydomain.com/UI/transaction.
BTW - templateUrl url's works fine in other place like UImodel so the problam is the navigation I think.
Set the location to the page $window.location.href = '#/transaction'; .
'#' indicating location the startpage which is normally index.html. so the link will be index.html#/transaction.Since its a single page app
i think in your config do
'/UI/transaction' instead of `/transaction`
and in the link do $window.location.href = '#/UI/transaction'
Is the mydomain.com/UI/transaction address correct (you use html5 mode)?
Does the problem occur when you try to enter directly into mydomain.com/UI/transaction?
If so, the problem is wrong server configuration (eg .htaccess file).
Try this
$location.path($window.location.origin +'/transaction');
And if you feel all the routes have something extra after the origin you can set a <base href="..."> for that and tell angular from where to take the rest amount of text for route purpose.
If you redirect the page with your_origin/transaction your browser will make a request to the server of /transaction, which the server will be not able to understand and 404 will come. But the route is client side (browser) routeing, so If you are routing to route /abc your javascript code is interpreting that, and invokes the corrosponding operations, (might be a different call to server for the html template or some data dependency, and then instantiating the controller and attach to view). So the moment you are trying to route your javascript code should be loaded.
Either you go for a ui-router where the route comes after # and this allow the browser to play after the text of (#) and not to reloading.
Or, make your server such a way, If a unidentified request comes you should return the base (index) page, so it will load all the scripts and that js will do the rest. (But obviously you will loose all the existing data in that state)
Related
My AngularJS project uses html5mode, I do these codes to open html5mode
In HTML:
...
<base href="/features-A/">
...
In JS:
app.config(['$locationProvider', function($locationProvider) {
$locationProvider.html5mode(true)
}])
NOTE: the /features-A/ is not an actual folder in my project, it is just a behavior defined in AWS CloudFront, Because we also have some URL /features-B point to other projects, We only need to know that: Whether it is accessed via "http://myhost.com" or "http://myhost.com/features-A/" is the entry file for my project: index.html
Here is what the browser does:
when going to my "Sign In" page, The URL in the address bar is
http://myhost.com/features-A/signin
When going to "Home" page, The URL in the address bar is
http://myhost.com/features-A/ome
As we see, AngularJS changed the URL via History API add /features-A/ inside to the URL
What I want
when people access "Sign In", The URL does not have /features-A/:
http://myhost.com/signin
When people access the "Home" page, The URL has /example:
http://myhost.com/features-A/home
This will make our website looks like the Sign In page is a system that is independent and outside /features-A/.
What I tried
I have tried some solutions however they haven't worked:
Set base href Dynamically In stateChangeSuccess event but URL still changed
Use HTML5 history API pushState and replaceState function, this way would cause page refresh indefinitely
So, Is there any solution to solve this problem?
how do not you create a new virtual host?
via php
php -S localhost:8080
type this Command in your folder's project
Many web frameworks such as AngularJS support "routing" whereby the user can visit the website, and have a template displayed to them based on their request URL. But these frameworks are entirely frontend JS, just simply a 'script src' import, so how does it manage to capture all requests to the website, and then redirect to a js file for processing, etc.
Any response is appreciated, since I have been trying to work out how exactly these frameworks execute the 'capturing' part of routing for some time, but with no luck.
Let me give you a simple model for it.
We can grab the current URL. We can then break that string and we can get different parts separated by '/' Based on which we can put condition which page and what data to show. And when we have decided that we can load our desired page using ajax.
For example my current url is https://www.example.com/module I can get "module" out of this url and then I can load inside the body of my page
$('body').load('module.html');
The following code is from my current work. See how different urls are captured and routed to my specified document. I also get the data from url to use into my ajax requests etc.
app.config(function($routeProvider) {
$routeProvider.when("/", {
templateUrl : "home.html"
}).when("/logout", {
templateUrl : "logout.html"
}).when("/course/:id", {
templateUrl : "course.html"
}).when("/course/:courseId/assignment/:assignmentId", {
templateUrl : "assignment.html"
});
});
Is it possible to serve a dynamic html page without a backend server or without using a front-end framework like Angular?
Edit
To clarify, the index file is served from a backend. This question is about how to handling routing between the index and dynamic pages.
I have an application that consists of two files - index.html and dynamic.html. When the user clicks an option say "Option A", they are served dynamic.html and the url is updated to /option-a. Now, with a server this is no problem and assuming the user visits the app from the landing page, it isn't a problem either because a cookie can be set. However, suppose a user visits a page at my-domain/option-a. That route doesn't exist and there is no server to redirect so it will 404. They would have to visit dynamic.html.
I think this architecture demands that there's either a server to handle route redirects or a SPA framework.
Is there something I'm missing?
your SPA framework will be active only once your HTML page is loaded and to do that you need to redirect any URL that user tries for your domain to that HTML file. For this you obviously need a server (and since you are talking about my-domain/option-a I assume you have atleast a basic server). You can refer to this link to get an idea on how server can redirect a URL to specific html file: Nodejs - Redirect url.
Once HTML is loaded you can initialize your SPA framework and decide the template to be loaded based on the URL.
Note: without a server you will access URLs using file://somepath/index.html and anything other than this URL will result in 404 and no SPA framework can handle that.
I think the solution is to use a static site generator such as Jekyll or Middleman and allows you to convert information into static pages. That way you functionally are building a bunch of pages but they are all compiled ahead of time. You can add dynamic content that is loaded in from a yaml file and it will compile the content into separate html pages.
It is not possible, but there is a workaround using url parameters like this:
my-folder/index.html
my-folder/index.html?=about
my-folder/index.html?=about/sublevel
my-folder/index.html?=profile
my-folder/index.html?=./games
const urlParams = new URLSearchParams(location.search);
const route = urlParams.get('');
console.log(route);
// Should print "about" "about/sublevel" "profile" "./games"
Of course this approach is not as clean as using a server for routing, but it's the best you can get without a server.
BTW. I tried an alternative solution creating symlinks with all the target routes pointing to the same index.htmlfile. But it did not work because the browser (firefox) redirects by default when it finds a symlink, thus home is shown all the time.
I have a homepage link that loads /register html page. But when I change css on the /register page and want to see it I have to go back to my localhost and then click the link again so the page loads again with new css. This is painfully time-consuming, is there a way to link /register with the page/route? Or at least remove /register from URL (so that localhost is only url for the whole app) so when the user refreshes the homepage welcomes him?
Homepage link:
REGISTER
View gets loaded like this
app.config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
$routeProvider
.when("/register", {
templateUrl: "/register",
controller: "registerController"
})
.otherwise("/");
$locationProvider.html5Mode(true);
}]);
This is an angular mechanic and is explained more in a recent question that I answered AngularJS + UI-Router - Manually Type URLs in HTML5 Mode without HashBang
Easy work around: use localhost/#/register instead to get to the page.
Since you are using
$location.html5mode(true);
This issue is directly related to how the files are being served to the browser. Your angular app itself only has one access point and that is your index.html page. When you type into your browser localhost/register, it is looking for the register directory, not the actual angular route. Since you've enabled html5 mode, it removed the hash bangs, which looks nice, but that requires additional configuration to be able to access the views individually without them.
Additional: If you want to remove the route URLs altogether, you will need to use stateProvider instead of routeProvider
Related article regarding stateProvider: Angular ui-router: Can you change state without changing URL?
Set in you html
<head>
<base href="/yor base url">
</head>
Say, I am browsing localhost/index.html#/view1, and I have a link pointing to localhost/index.html . When I click it, angular process it through its ng-route module and it falls into my redirect clause.
this is an extract from the route config:
$routeProvider
.when( "/view1", route1 )
.otherwise({redirectTo:'/view1'}); // localhost/index.html clicks fall here
I could use a ng-click and then redirect javascriptly with location.href, but this is not a desirable solution because I would lose the href link(SEO, bookmark, tabs).
tl;dr
How do I tell angularJs ng-route not to process urls without hash ?
EDIT: I do not user html5 mode and do not plan on using it.
If you are asking about angular routing service ignoring the url links. See documentation of $location
In cases like the following, links are not rewritten; instead, the
browser will perform a full page reload to the original link.
Links that contain target element
Example: link
Absolute links that go to a different domain
Example: link
Links starting with '/' that lead to a different base path when base is defined
Example: link