I do not understand why my routes are not working except when I remove the "/" at the beginning.
Here's my routes:
app.config(function($locationProvider, $routeProvider)
{
$locationProvider.html5Mode(true);
$routeProvider
.when('/', { templateUrl: 'templates/views/home.html', controller: 'HomeController' })
.when('/about', { templateUrl: 'templates/views/about.html', controller: 'AboutController' })
.when('/contact', { templateUrl: 'templates/views/contact.html', controller: 'ContactController' })
.otherwise({ redirectTo: '/404' });
});
and my html look like this:
<!DOCTYPE html>
<html lang="en">
<head>
<base href="/angular/">
<meta charset="utf-8">
<title>AngularJS Demo</title>
<link rel="stylesheet" href="css/app.css">
</head>
<body ng-cloak ng-app="app">
<ul>
<li>Home</li>
<li>About</li>
<li>Contact</li>
</ul>
<div ng-view></div>
<script src="js/libs.js"></script>
<script src="js/app.js"></script>
</body>
</html>
So if I go to
http://example.com/angular/
everything goes as excepted but if i click on the "Contact" link, it will bring me to
http://example.com/contact
instead of
http://example.com/angular/contact
Any idea what is going wrong with my code?
P.S. I also have the following .htaccess
RewriteEngine On
Options FollowSymLinks
RewriteBase /angular/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.html [QSA,NC,L]
Thanks
Yes of course, here it is:
Let me know if you need anything else. Please note that this is the content of my "angular folder", which is at the root of my server "public_html" root.
Thanks
Related
I try remove # in url . It's working fine before i remove it . But when i remove , I get err in console cant get file patch . Here is my code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<base href="/">
</head>
<body>
<a ui-sref="main">Main</a>
<a ui-sref="login">Login</a>
<div ui-view>
<script src="test.js"></script>
<script src="testroute.js"></script>
<script src="testservices.js"></script>
</body>
</html>
Here is js config
function configRoute($stateProvider,$locationProvider){
$locationProvider.html5Mode(true);
$stateProvider
.state('main',{
url:'/main',
templateUrl:'test2.html',
controller: 'myController',
controllerAs:'vm'
})
.state('login',{
url:'/login',
templateUrl:'test1.html',
controller:'testController',
controllerAs:'vm'
})
Here is err i get
GET http://localhost:8080/test.js 404 (Not Found)
GET http://localhost:8080/testroute.js
GET http://localhost:8080/testservices.js
Try to give solution folder name in base tag as example below,
<base href="/SolutionFolder/">
i have an angularJS app using ui-router, here my config file content:
app
.config(['$stateProvider', '$urlRouterProvider', '$locationProvider', function($stateProvider, $urlRouterProvider, $locationProvider) {
if(window.history && window.history.pushState) {
$locationProvider.html5Mode({
enabled: true,
requireBase: false
});
}
$urlRouterProvider
.when('/', 'login')
.otherwise('/error404');
$stateProvider
.state({
name: 'main',
abstract: true,
templateUrl: '/templates/navbar.html',
controller : 'NavBarController',
})
.state({
name: 'main.login',
url: '/login',
templateUrl: '/templates/login.html',
controller : 'LoginController as login',
data: {
authenticate: false,
admin: false,
navBar: false,
}
});
}]);
Also i have a .htaccess file containing:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
RewriteRule ^ /index.html
My folder structure is as below:
- www
- mP
- mPc
-.htaccess
-/js (containing all angularJS files)
-/templates (containing all the template files)
- index.html
And my index.html file has the following content:
<!DOCTYPE html>
<html ng-app='App'>
<head>
<title>App</title>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<script src="//code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.0-beta.5/angular.min.js"></script>
<script type="text/javascript" src="./js/angularJS/dependencies/angular-ui-router.min.js"></script>
<script type="text/javascript" src="./js/angularJS/app.js"></script>
<script type="text/javascript" src="./js/angularJS/constants.js"></script>
<script type="text/javascript" src="./js/angularJS/directives.js"></script>
<script type="text/javascript" src="./js/angularJS/filters.js"></script>
<script type="text/javascript" src="./js/angularJS/services.js"></script>
<script type="text/javascript" src="./js/angularJS/controllers.js"></script>
<script type="text/javascript" src="./js/angularJS/config.js"></script>
<script type="text/javascript" src="./js/angularJS/run.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
</head>
<body ng-controller='MainController'>
<div class="container">
<div class="row">
<div class="center">
<section ui-view></section>
</div>
</div>
</div>
When i try to access the site using localhost/mP/mPc/ it goes to localhost/error404 and when i use localhost/mP/mPc/login it shows an error saying "Not Found The requested URL /index.html was not found on this server"
Any help would be highly appreciated!
For login
$urlRouterProvider
.when('/', '/login')
Since you are using templates from same folder you can use relative path
templateUrl: 'templates/navbar.html',
I've been looking at ways to remove the # from the URLS within AngularJS and found many solutions which pointed me in the direction to use what they had suggested now Inside my app.js I have the following:
var app = angular.module('mainApp', ['ngRoute', "oc.lazyLoad"]);
app.config(function ($routeProvider, $locationProvider) {
$routeProvider
.when('/home',
{
templateUrl: '/Templates/Home/home.html',
})
.when('/About',
{
templateUrl: '/Templates/About/About.html'
})
.when('/home/:page_number',
{
templateUrl: '/Templates/Params/params.html',
resolve: {
loadMyCtrl: ['$ocLazyLoad', function ($ocLazyLoad) {
return $ocLazyLoad.load('Templates/Params/paramsController.js');
}]
}
})
.otherwise('/home',
{
templateUrl: '/Templates/Home/home.html'
});
$locationProvider.html5Mode({
enabled: true,
requireBase: false
});
});
Inside the head of my SiteMaster.Html I've placed
<base href="/">
Full breakdown:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="Scripts/angular.min.js"></script>
<script src="Scripts/angular-route.min.js"></script>
<script src="Scripts/app.js"></script> <!-- Consists of Routing -->
<script src="Scripts/ocLazyLoad.js"></script>
<title>Angular Routing Tutorial</title>
<base href="/">
</head>
<body ng-app="mainApp">
<h1>sitemaster Template</h1>
<div ng-view></div>
</body>
</html>
Yet when I run this I get directed to /home when I manipulate the URL to be /About I get
HTTP Error 404.0 - Not Found
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
And the same for /home/SomeValue
Yet when I remove
$locationProvider.html5Mode({
enabled: true,
requireBase: false
});
And remove base href="/" from my SiteMaster.Html the routes work as expected?
Can someone explain to me why this behavior would be happening?
Using ui-router with AngularJS application, It don't render the views correctly.
That's the app.js file implementation:
'use strict';
angular.module('myApp', [
'ngCookies',
'ui.router'
]);
angular.module('myApp').config(['$stateProvider', '$urlRouterProvider', '$locationProvider', '$httpProvider',
function($stateProvider, $urlRouterProvider, $locationProvider, $httpProvider){
// Login Route
$stateProvider.state('login', {
url: '/login',
templateUrl: '/app/views/login.html',
controller: 'loginController'
});
$stateProvider.state('home', {
url: '/home',
templateUrl: '/app/views/home.html'
});
$urlRouterProvider.otherwise('/home');
$locationProvider.html5Mode(true);
}]);
And that's the index.html:
<!DOCTYPE html>
<html class="no-js" lang="en" data-ng-app="myApp">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="./vendor/bootswatch/bootstrap.css" media="screen">
<link rel="stylesheet" href="./vendor/bootswatch/bootswatch.min.css">
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css">
<link rel="stylesheet" href="./css/style.css">
<script src="./bower_components/angular/angular.min.js"></script>
<script src="./bower_components/angular-route/angular-route.min.js"></script>
<script src="./bower_components/angular-cookies/angular-cookies.min.js"></script>
<script src="./bower_components/angular-ui-router/release/angular-ui-router.min.js"></script>
<script src="./app/app.js"></script>
<script src="./app/modules/login/module.js"></script>
<script src="./app/modules/home/module.js"></script>
</head>
<body data-ng-cloak>
<div class="container" data-ui-view></div>
<script src="./vendor/jquery/jquery-1.10.2.min.js"></script>
<script src="./vendor/bootstrap/js/bootstrap.min.js"></script>
<script src="./vendor/bootswatch/bootswatch.js"></script>
</body>
</html>
The problem is that when starting the app and passing to the browser this url (localhost:8000/login) or (localhost:8000/home), It responds with (Not Found)
The cause of this problem is that the URL don't contain the '#'.
Example:
[localhost:8000/#/home] --> works well
[localhost:8000/home] --> don't work at all and shows "Not Found"
The problem is with your .html5Mode(true). You need to update your .htaccess. It all depends on your application, but here is an example of what you can do. This has worked for many Apache users, so hopefully, it will work for you as well.
You also need to take care of the the known <base href> bug found here
<VirtualHost *:80>
ServerName my-app
DocumentRoot /path/to/app
<Directory /path/to/app>
RewriteEngine on
# Don't rewrite files or directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# Rewrite everything else to index.html to allow html5 state links
RewriteRule ^ index.html [L]
</Directory>
</VirtualHost>
Source: This post.
I am developing the angular app, when i hit the browser with the url: http://localhost/angular/MyAngularRewind/ , the index.html is not opening but actually it should have opened the index.html.
C:\wamp\www\Angular\MyAngularRewind ---> this is the path where i am having the index.html, see screenshot below
but page is opening when i hit with this url: http://localhost/angular/MyAngularRewind/index.html and it changes to http://localhost/angular/MyAngularRewind/index.html#/
ideally below thing should have happened: when i hit the url:http://localhost/angular/MyAngularRewind/ it should open the page with the url automatically changes to http://localhost/angular/MyAngularRewind/index.html/#/, but it is not happening i got no clue, why this happens.
see the screenshot for file structure.
Index.html
<html lang="en" ng-app="myApp">
<head>
<meta charset="utf-8">
<title>My HTML File</title>
<title>Google Phone Gallery</title>
<link rel="stylesheet" href="css/app.css">
<link rel="stylesheet" href="css/bootstrap.css">
<script src="lib/angular/angular.js"></script>
<script src="js/controllers.js"></script>
<script src="js/routes.js"></script>
</head>
<body ng-controller="appCtrl">
<div class="" ng-view></div>
</body>
</html>
Javascript:
angular.module('myApp', []).
config(['$routeProvider', function($routeProvider) {
$routeProvider.
when('/',
{
templateUrl: 'partials/partial-For-routes-1.html',
controller: appCtrl
}).
when('/page-2',
{
templateUrl: 'partials/partial-For-routes-2.html',
controller: appCtrl
}).
when('/page-3',
{
templateUrl: 'partials/partial-For-routes-3.html',
controller: appCtrl
}).
otherwise({redirectTo:("/")})
}]);
Thanks in advance for any help.
The problem is probably with your server and not Angularjs - You need to instruct the server to automatically serve index.html when you access a directory (/angular/MyAngularRewind/.
Having said that I'm not sure which server your using, so I can't offer a solution