Adding $locationProvider to config causing error - angularjs

Im just learning angular, When i add $locationProvider to my .config, im getting the following error. Removing any reference to $locationProvider removes the error. Is this some angular version error, i have tried different (higher) versions? Im not sure how to add the $locationProvider in without getting the error. Any help would be appreciated.
Ive set up in index.html
Error: Error: [$injector:modulerr] http://errors.angularjs.org/1.2.9/$injector/modulerr?p0=styleApp&p1=%5B%24injector%3Anomod%5D%20http%3A%2F%2Ferrors.angularjs.org%2F1.2.9%2F%24injector%2Fnomod%3Fp0%3DstyleApp%0AF%2F%3C%40https%3A%2F%2Fajax.googleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.2.9%2Fangular.min.js%3A6%3A449%0AVc%2Fb.
My config looks like:
styleApp.config(function($routeProvider, $locationProvider) {
$routeProvider
// route for the home page
.when('#/', {
templateUrl : '/page/page1.html',
controller : 'mainCtrl'
})
// route for 2nd page
.when('#page2', {
templateUrl : '/subdir/page2.html',
})
// route for the 3rdpage
.when('#page3', {
templateUrl : '/subdir/page3.html',
})
.when('#page4', {
templateUrl : '/subdir/page4.html',
})
.when('#page5', {
templateUrl : '/subdirn/page5.html',
});
// use the HTML5 History API
$locationProvider.html5Mode(true);
});

You should add 'ngRoute', as a dependency to your module like this:
var styleApp = anular.module('moduleName', ['ngRoute']);
Secondly, html5Mode makes your URLs look like normal URLs, so you should remove the # from all the routes; consequently, the route for page 4 will look like this:
.when('/page4', {
templateUrl : '/subdir/page4.html',
})

Related

'/:routeparams' is overriding other route in angularjs

app.config(function($routeProvider) {
$routeProvider
.when('/', {
templateUrl : 'junk.html',
})
. .when('/:pageuniqueid', {
templateUrl : 'page.html',
})
// route for the about page
.when('/first', {
templateUrl : 'first.html',
})
// route for the contact page
.when('/second', {
templateUrl : 'second.html',
});
});
If i type 'example.com/first' in the URL then instead of getting first.html i am getting page.html.
I am implementing the page that user can access directly with their dynamic pageid after base URL.
I want to get page.html only if it is not matched with the other routes. Is there a way to achieve this?
The order of the route definitions matters.
If you define the '/first' route before the '/:pageuniqueid', it should work.
The angular router stops evaluating the routes after the first match.
So in order to get the page.html as a fallback, you should put it as last entry in the list.

angular route getting problems

i'm trying to use ngRoute to config my routes on my app but for some reason, it still not working. i've searched a lot and seems my code is ok. i'm gonna show how i'm doing it:
my a.href:
<a href="#/bancodedados">
my config route:
academico.config(function($routeProvider){
var home = {
controller : "home",
templateUrl : "js/plugins/angular/views/home.html"
}
var bancodedados = {
controller : "bancodedados",
templateUrl : "js/plugins/angular/views/bancodedados.html"
}
$routeProvider
.when("/", home)
.when("/bancodedados", bancodedados);
});
but for some reason the app redirect me to http://localhost/joli/#!/#%2Fbancodedados
and still on the same views.
This is often because of upgrading angular to version 1.6 which changes the default hash prefix to ! whereas it used to be '' (the empty string). You can read more on this here and here.
Potential Fix 1: Change your links to use #! (hashbang) as follows:
<a href="#!/bancodedados">
Potential Fix 2: reset the hash prefix back to the empty string by injecting $locationProvider into your config block and then setting the hash prefix as follows:
academico.config(function($routeProvider, $locationProvider){
$locationProvider.hashPrefix('');
// The rest of your config block...
});

angularjs routing - stay on page and do nothing on "otherwise" method

I need to set Angularjs routing to do nothing on "otherwise" method.
var townApp = angular.module('townApp', ["ngRoute"]);
townApp.config(function($routeProvider){
$routeProvider
.when("/dashboard", {
templateUrl : "/profile/dashboard/"
})
.when("/payments", {
templateUrl : "/profile/payments/",
})
.otherwise(
/* DO NOTHING.*/
)
});
Right now, it cleans the ng-view directive upon changing url to an undefined one and that's not what I need.
How can I make it stay on the same page and do nothing?
I found this Here.
I have no idea what is happening but it works as i want.
townApp.config(function($routeProvider){
$routeProvider
.when("/dashboard", {
templateUrl : "/profile/dashboard/"
})
.when("/payments", {
templateUrl : "/profile/payments/",
})
.otherwise({redirectTo: $routeProvider});
});

AngularJS - simple routing not working

I have a route with parameter like this
var app = angular.module("myapp", ["ngRoute"]);
app.config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider) {
$locationProvider.hashPrefix("!").html5Mode(true);
$routeProvider
.when('/sukien', { templateUrl: '/app/views/sukien/index.html' , controller: 'eventCtrl' })
.when('/sukien/:id', { templateUrl: function (params) { return '/app/views/sukien/index.html?id=' + params.id }, controller: 'eventCtrl' })
}])
why /sukien works and /sukien/:id doesn't ? indeed, angularjs seems not to understand what it is. "Uncaught TypeError: undefined is not a function"
/sukien/333 => failed to work.
You are mixing template url, state of your application and search parameters it seems.
The templateUrl tells angularjs where to look for the html file : it will very unlikely depend on the params.id and be set via a function, but will rather be a constant.
It has nothing to see with the url that the user sees in their browser.
For example : '/app/views/sukien/suiken.html'
The url the user sees will be something like :
.../suiken/1223445
And you can then access the id in your controller via $routeParams.id

Angular JS deep linking $routeProvider reload not working

I have the following config for routes:
app.config( function ($routeProvider, $locationProvider, $httpProvider) {
// Routes
$routeProvider.when('/admin', {
templateUrl : '/app/partials/dashboard.html',
controller : 'DashboardCtrl'
});
$routeProvider.when('/admin/settings', {
templateUrl : '/app/partials/settings.html',
controller : 'SettingsCtrl'
});
$routeProvider.when('/404', {
templateUrl : '/app/partials/404.html',
});
$routeProvider.otherwise({redirectTo: '/404'});
$locationProvider.html5Mode(true);
});
Everything is working except, when I'm on /admin/settings and reload the page, it redirects to 404. I tried removing the html5Mode, it works. However, I really want to use html5Mode. What am I missing? Please help me.
Oh, btw, I'm using AngularJS 1.1.5.
I had a very similar error and solved it by inserting <base href='my_app_base_url'></base> into my index.html page, in the head. The idea came from this GitHub issue (https://github.com/angular/angular.js/issues/2774) and this other one (https://github.com/angular/angular.js/issues/2868), as well as the one I mentioned in my comment.
try $location.path('/404').replace();

Resources