AngularJS router doesn't actually route much - javascript

So I'm fresh into AngularJS, trying to build my first application. And I'm stuck at routing. The first line works, which is just to load a view when the site is entered, but the next one is just telling me "Object not found".
Now I'm a true noob. I'm just running this on a plain MAMP stack.
This is the code I've written in JS:
angular.module("Portfolio", ['ngRoute', 'ngProgress']);
angular.module("Portfolio").config(function($routeProvider, $locationProvider) {
$routeProvider.when('/', {
templateUrl: '/assets/pages/home.php',
controller: 'MainController'
}).when('/test', {
templateUrl: "assets/pages/test.php",
controller: 'MainController'
});
$locationProvider.html5Mode(true);
});
I'm certain that there's no mistake in HTML, since the rest of the code works just fine.
So what did I do wrong here? Thanks.

angular.module("Portfolio", ['ngRoute', 'ngProgress']);
angular.module("Portfolio").config(function($routeProvider, $locationProvider) {
$routeProvider.when('/', {
templateUrl: '/assets/pages/home.php',
controller: 'MainController'
}).when('/test', {
templateUrl: "/assets/pages/test.php",/* your previous template url is not correct.*/
controller: 'MainController'
});
$locationProvider.html5Mode(true);
});

insert / in this routing
templateUrl: "/assets/pages/test.php",

Related

Angular routing otherwise

I'am writing an homepage using javascript, an express server and angular.
Currently I have following routing:
function config($routeProvider) {
$routeProvider
.when('/', {
templateUrl: '/login/login.view.html',
controller: 'loginCtrl',
controllerAs: 'vm'
})
.when('/home', {
templateUrl: '/home/home.view.html',
controller: 'homeCtrl',
controllerAs: 'vm'
})
.otherwise({redirectTo: '/'});
}
Instead of the redirection in .otherwise to login page I would like that the user gets an page like:
Not found
The requested URL <requestedURL> was not found on this server.
How can I get the requested url in the controller of redirected page?
Or what is the common way to implement this?
Kind regards,
Wolfgang

html5mode breaks AngularJS routing

I am having an issue with using html5Mode with ngRoute not quite like issues others have had with the same thing. Here is the most relevant section of my code:
(function () {
var config = function ($routeProvider, $locationProvider) {
$routeProvider
.when('/', {
templateUrl: 'home/home.view.html',
controller: 'homeCtrl',
controllerAs: 'vm'
})
.when('/about', {
templateUrl: 'common/views/genericText.view.html',
controller: 'aboutCtrl',
controllerAs: 'vm'
})
.when('/location/:locationId', {
templateUrl: 'locationDetail/locationDetail.view.html',
controller: 'locationDetailCtrl',
controllerAs: 'vm'
})
.otherwise({redirectTo: '/'});
/* $locationProvider.html5Mode({
enabled: true,
requireBase: false
}); */
};
angular
.module('loc8r', ['ngRoute', 'ngSanitize'])
.config(['$routeProvider', '$locationProvider', config]);
})();
The issue arises specifically with the route for /location/:locationId. With html5Mode on, an attempt to load a URI like http://127.0.0.1:3000/location/5a27ab691c5e0a989c0634da results in a blank page. Curiously, my logging output from Node tells me that the template and controller are both being accessed. Even more curiously, if I change the route from /location/:locationId to just /:locationId and load a URI like http://127.0.0.1:3000/5a27ab691c5e0a989c0634da, then the page will load. But the only way to get /location/:locationId to work is to leave html5Mode disabled and then go to http://127.0.0.1:3000/#!/location/5a27ab691c5e0a989c0634da. But that's ugly. How can I get html5Mode working in this case?
If necessary, I can push all my most recent changes to GitHub and then provide the full code.
Have you set up base attribute in index.html file?
Check: How to setup AngularJS $locationProvider HTML5 mode for non-root base urls?

Angular dynamic controller / templateUrl

I am trying to build my first angular app and looking to setup dynamic routing so that I can build out the app without having to continuously add to the $routeProvider config. I haven't quite found the simplest / cleanest approach yet, any guidance is much appreciated. Below was my first approach.
angular.module('app', ['ngRoute']).config(['$locationProvider', '$routeProvider', function($locationProvider, $routeProvider){
$locationProvider.html5Mode(true).hashPrefix('!');
$routeProvider
.when('/', {
redirectTo: '/home',
})
.when('/:section', {
controller: function(r){
return r.section+'Ctrl';
},
templateUrl: function(r){
return 'app/views/pages/'+r.section+'.html';
}
});
}]);

Angular - routing why do I getting my controller 2 times called

I am using routing in angular app. while the page loads, I am getting the controller function triggers 2 times..
How to avoid that or is it have any useful meaning to call 2 times?
any one help me and explain the mistake what i do?
here is my html: //i use jade!
header
h1 Header
div.content(ng-controller="HomeController")
div(ng-view)
footer
h5 Footer
here is my js:
var locations = angular.module('location', ['ngRoute']);
locations.config(['$routeProvider', function($routeProvider) {
$routeProvider
.when('/', {
controller: 'HomeController',
templateUrl: 'views/home.html'
})
.when('/inbox/:name', {
controller: 'InboxController',
templateUrl: 'views/inbox.html'
})
.otherwise({redirectTo: '/'});
}]);
locations.controller('HomeController', ['$scope', function($scope){
console.log('hi'); // i am getting 2 times consoled! -why?
}]);
Because you have div.content(ng-controller="HomeController") in your template. You don't need to explicitly define ng-controller directive to your header template because It has already been associated using $routeProvider

AngularJS app URL

I am using ngRoute in my AngularJS app to build up different app routes, below is part of my app.js. Now the problem I am facing is that the URL is shown as ( localhost/myapp/index.html#/home ) while my client needs is at localhost/myapp#/home so I was wondering how I can get the index.html out of the equation or at least make it as localhost/myapp/index#/home without the .html? Thanks
var myApp = angular.module('myApp', ['ngRoute','ngSanitize']).
config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/home',
{ templateUrl: 'templates/home.html',
controller: 'homeController'
});
$routeProvider.when('/about',
{templateUrl: 'templates/aboutus.html',
controller: 'aboutUsController'
});
$routeProvider.otherwise({redirectTo: '/home'}); }]);
You should be able to do this with a combination of $locationProvider.html5Mode(true) and <html><head><base href="/myApp">

Categories