I just got started with angular js , i tried routing and worked pretty well .The problem is when i refresh the page it is showing 404 error since it is checking in my actual directory rather than checking in routes.
here is the complete code
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-route.js"></script>
<body ng-app="myApp">
<base href="/" />
<a href="blue">blue</a>
<a href="green">green</a> //blue.html and green.html are in same directory as index.html
<p>Click on the links to load blue or green.</p>
<div ng-view></div>
<script>
var app = angular.module("myApp", ["ngRoute"]);
app.config(function($routeProvider, $locationProvider) {
$routeProvider
.when("/blue/:name?", {
templateUrl: "blue.html",
controller: "blue_control"
})
.when("/green", {
templateUrl: "green.html",
controller: "green_control"
})
$locationProvider.html5Mode({
enabled: true
});
});
app.controller("blue_control", function($scope) {
$scope.msg = "this is blue";
});
app.controller("green_control", function($scope) {
$scope.msg = "this is green";
});
</script>
</body>
</html>
here are the browser outputs
this is when i refresh
please provide me with some solution.
The problem is that you are not assigning the variable,$routeProvider to your controller, this causes the controller not to be able to access the variable $ routeParams
try it
I think you can not use a route version with a lower version of angulajs please change a upper version of angularjs for example
HTML5 mode requires URL rewriting.
From the Docs:
For NodeJS and ExpressJS
For IIS on Windows
See, How do I configure IIS for URL Rewriting an AngularJS application in HTML5 mode?
For apache
See, How to rewrite url in apache htaccess for angularjs app
For more information
Article: AngularJS - Enable HTML5 Mode Page Refresh Without 404 Errors in NodeJS and IIS.
I had the same problem using http-server as my local webserver, its a known issue with html5 and detailed in the angular-ui/ui-router FAQ.
I switched to Nginx (install via apt-get) with the following
/etc/nginx/sites-enabled/default
To start/stop/reload after changes;
The FAQ also covers Apache/Express/asp.NET