I'm not clear why this isn't functioning. The rest of my code - sans the URL Param-based routing is working fine, I've tested that all. However, when I tried to include a URL argument in my url (base.url/route/:param) the '.otherwise' element of my routeProvider is firing instead of the appropriate controller designated in the routeProvider.
Here's the relevant bits of code:
module.config
app.config(function($routeProvider){
//http://docs.angularjs.org/tutorial/step_07
$routeProvider
.when('/home',
{
templateUrl: 'app/partials/home.html',
controller: 'HomeController'
})
.when('/implementation-reference/:ref-scope',
{
templateUrl: 'app/partials/topic_tpl.html',
controller: 'ImplReference'
})
.when('/projects',
{
templateUrl: 'app/partials/project_list_tpl.html',
controller: 'Projects'
})
.when ('/site-help',
{
templateUrl: 'app/partials/site-help.html'
})
.otherwise(
{
templateUrl: 'app/partials/404.html'
})
});
module.controller
app.controller('ImplReference', function($scope, $routeParams) {
alert('hi');
});
I get my 404 page when going to /implementation-reference/whatever.
Any ideas why I don't see the alert I put at the beginning of my controller, nor see any errors in the console?
As best as I can tell, the issue lay in use a dash in the param as defined in RouteProvider. I originally had:
.when('/implementation-reference/:ref-scope',
{
templateUrl: 'app/partials/topic_tpl.html',
controller: 'ImplReference'
})
when I changed it to this:
.when('/implementation-reference/:ref',
{
templateUrl: 'app/partials/topic_tpl.html',
controller: 'ImplReference'
})
...suddenly everything works fine.
I noticed this after adding a controller to the 'otherwise' that showed me the $route information. After drilling through it, I saw that the regex on the pre-defined route which had the param was only working on the part before the dash.
I've been mystified by this for hours, and can't imagine how this isn't documented more clearly somewhere. Perhaps this is some newbie mistake (I am new to this).
Related
My oversimplified app.config() has:
$stateProvider.
state("/", {
url: "/",
templateUrl: "main.html"
}).
state("/newCategories", {
url: "/categories/new",
templateUrl: "/views/new_categories.html",
controller: "newCategoriesCtrl"
}).
state("/categoryPages", {
url: "/categories/:address",
templateUrl: "/views/categories.html",
controller: "categoriesCtrl",
resolve: {
categoriesDataResolve: function resolveTemplate($stateParams, DataResolver) {
return DataResolver.resolveTemplates($stateParams.address);
}
}
});
With this I can use ui-serf link with "/newCategories" to load its url: "/categories/new"
<a ui-sref="/newCategories">New Category</a>
However, when I refresh, it thinks that "/new" is part of $stateParams. Therefore it uses a different controller and tries to resolve its template (which is missing, so it gives an error).
For now I fixed it by changing the url from "/categories/new" to "/categories-new" so it won't get confused on refresh. But how do I solve this issue differently? (Maybe ui-router has a some way of dealing with it)
If I understand you right, you want to call different controller a.e. newCategoriesCtrl when user calls /categories/:address where address param is new
Changing /categories/new to "/categories-new is a right way to solve it.
Small tip: its not good practice to use / as prefix for state name. It confuses the developer and can be mixed with original URL.
$stateProvider.
//...
state("newCategories", {
url: "/categories-new",
templateUrl: "/views/new_categories.html",
controller: "newCategoriesCtrl"
}).
state("categoryPages", {
url: "/categories/:address",
templateUrl: "/views/categories.html",
controller: "categoriesCtrl",
resolve: {
//...
}
});
I'm building my portfolio as a front-end dev and it's still under developmentt. To test it out I used FTP to upload the files to my justhost domain which is a success. Now, say my site is www.johndoe.com (okay fine, my actual site is 'hassanefall.com' to make this easier to understand :)).
(I'm using angularjs for the routes) I want to go to a specific route/page: www.hassanefall.com/about. The about page works when i navigate through clicking the links.
But, when I type in the url the exact url path, there's a 404 error page.
I have '/work', '/contact', etc. and whenever I type in the url 'www.hassanefall.com/contact' directly without navigating to it, a 404 page occurs. It's as if the file for each page/route doesn't exist.Help?
Here's the code.
Note: I removed the hash from the url using base '/' and $relocationProvider. If there's an answer to this it's greatly appreciated.
var app = angular.module('mySite', ['ngRoute']);
app.config(function($routeProvider, $locationProvider){
$routeProvider
.when('/', {
controller: 'HomeController',
templateUrl: 'views/home.html'
})
.when('/projects', {
controller: 'HomeController',
templateUrl: 'views/projects.html'
})
.when('/service', {
controller: 'HomeController',
templateUrl: 'views/service.html'
})
.when('/about', {
controller: 'HomeController',
templateUrl: 'views/about.html'
})
.when('/contact', {
controller: 'ContactController',
templateUrl: 'views/contact.html'
})
.when('/portfolio/:id', {
controller: 'ItemController',
templateUrl: 'views/item.html'
})
.otherwise({redirectTo: '/'});
$locationProvider.html5Mode(true);
});
If you need more code sample please let me know. :)
Your server should support HTML5 mode. What is happening here is that the server is looking for your client side route ('contact', for example), and does not find it. That's why you have 404 not found error.
Another solution is to go back to hash mode..
I am having a problem with AngularJS (more like the routing) and any version of IE. It comes to the point where Chrome, Firefox and Safari work fine, without a pinch of problems. But when I start to work with IE ... everything goes wrong.
Normally when I navigate to the URL of localhost, it start with "localhost:52201" and changes to "localhost:55201/#/ExpenseOverview" which is indeed the first 'page/view' that is loaded, according to my AngularApp (code below)
var angularApp = angular.module('AngularApp', ['ngRoute']);
angularApp.config(['$routeProvider',
function ($routeProvider) {
$routeProvider
.when('/ExpenseOverview', {
controller: 'ExpenseOverviewController',
templateUrl: 'Angular/Views/ExpenseOverview.aspx'
})
.when('/AddExpense/', {
controller: 'AddExpenseController',
templateUrl: 'Angular/Views/AddExpense.aspx'
})
.when("/AddExpense/:update", {
controller: 'UpdateExpenseController',
templateUrl: 'Angular/Views/AddExpense.aspx'
})
.when('/ApproveExpense', {
controller: 'ExpenseOverviewController',
templateUrl: 'Angular/Views/ExpenseOverview.aspx'
})
.when('/TypeGrid', {
controller: 'TypeGridController',
templateUrl: 'Angular/Views/TypeGrid.aspx'
})
.when('/TypeGrid/:updated', {
controller: 'TypeGridController',
templateUrl: 'Angular/Views/TypeGrid.aspx'
})
.when('/AddType', {
controller: 'AddTypeController',
templateUrl: 'Angular/Views/AddType.aspx'
})
.when('/AddType/:update', {
controller: 'UpdateTypeController',
templateUrl: 'Angular/Views/AddType.aspx'
})
.when('/ProjectGrid', {
controller: 'ProjectGridController',
templateUrl: 'Angular/Views/ProjectGrid.aspx'
})
.when('/ProjectGrid/:updated', {
controller: 'ProjectGridController',
templateUrl: 'Angular/Views/ProjectGrid.aspx'
})
.when('/AddProject', {
controller: 'AddProjectController',
templateUrl: 'Angular/Views/AddProject.aspx'
})
.when('/AddProject/:update', {
controller: 'UpdateProjectController',
templateUrl: 'Angular/Views/AddProject.aspx'
})
.when('/Organogram', {
controller: 'OrganogramController',
templateUrl: 'Angular/Views/Organogram.aspx'
})
.otherwise({ redirectTo: '/ExpenseOverview' });
}]);
Just to point this out, I know I am working with .aspx-files instead of static .html-files, but this doesn't matter right now, because it's of no concern to this problem.
As I stated before, this problem only occurs when using IE. It doesn't matter what version I am using, from IE8 to IE11, none of those are doing the routing and showing views.
I have tried multiple solutions, such as adding certain -tags to the head, adding an id="ng-app" to my -tag and so on, from which none of them resulted in a view.
Something is unexplainable to me is that when I debug my project with Visual Studio and Internet Explorer, everything works fine, like there was never any problem at all. After I published my website and put it online .. well, it's quite clear what that result is (otherwise I wouldn't be posting this).
I seriously need help with this issue. I need to be able to deploy my website to both PC, iOS, Android and Windows Surface. Every platform-browser but IE works and I cannot find a plausible solution for this.
So I thought someone out there knew the cause, might know a solution or maybe a workaround/fix for me?
Thanks in advance!
I'm working on a AngularJS + OnsenUI project, and I'm having problems with the navigation.
Let's say that I have a module:
angular
.module('app.home', ['ui.utils','ngRoute','ngAnimate'])
.controller('HomeCtrl', HomeCtrl)
.config(function($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'path/to/home/template',
controller: 'HomeCtrl'
})
.when('/test1', {
templateUrl: 'path/to/template',
controller: 'TestOneCtrl'
})
.when('/test2', {
templateUrl: 'path/to/template',
controller: 'TestTwoCtrl'
})
.otherwise({
redirectTo: 'path/to/home/template'
});
});
In the HomeCtrl I'm supposed to (depending on the result of certain functions) navigate to either test1.html or test2.html. My problem is that I don't know how to link the routeProvider to the the ons.navigator.pushPage function.
This doesn't work:
var url = '/test1';
$scope.navigator.pushPage( url, { animation : 'slide' } );
This works:
var url = '/absolute/path/to/template';
$scope.navigator.pushPage( url, { animation : 'slide' } );
My question is what do I need to do so I don't have to write the absolute path to the template in the url variable? Apparently I'm missing out on something, but I can't figure out what.
Thanks in advance!
I think it's because the path used in $routeProvider is not the same type of that of pageUrl used in navigator.pushPage().
$routeProvider.when(path, route);
and
navigator.pushPage(pageUrl, option);
Path is like the pattern or string of your app url found in the browser address bar. For example, "http://localhost:8000/app/index.html#/test1". That's when you can refer to this in the routeProvider as "/test1". However, in the navigator.pushPage(), you will need to specify exact url to the page just like how you set ur templateUrl inside $routeProvider. In other words, pageUrl = route.
That's just from my understanding though.
I'm trying to debug the above code which I feel like this is very useful >.<
I can't even find what is wrong in my code and don't really know where to start since Angularjs still very new to me.
I'm trying to localize Angularjs app. I know this is missing a lot of context in order to get a help, but I'm trying to see what information that I should give that would help on this debugging.
I've gone through any indexOf in angularjs file and I can see few things is undefined but don't know if that would be helpful.
So I've traced the problem here and this is what I have figured is the problem, but still counldn't figure out why...
config(['$routeProvider', '$locationProvider',
function ($routeProvider, $locationProvider) {
$routeProvider.when('/:locale?/:username/badges', {
templateUrl: '_partials/badges.html',
controller: 'badges'
})
.when('/:username/badges', {
templateUrl: '_partials/badges.html',
controller: 'badges'
})
.when('/:username/teaching-resources', {
templateUrl: '_partials/teaching-resources.html',
controller: 'teachingResources'
})
.when('/:username/makes', {
templateUrl: '_partials/makes.html',
controller: 'makes'
})
.when('/:username/likes', {
templateUrl: '_partials/likes.html',
controller: 'likes'
})
.when('/:username/events', {
templateUrl: '_partials/events.html',
controller: 'events'
})
.when('/:username', {
templateUrl: '_partials/badges.html',
controller: 'badges'
});
$routeProvider.otherwise({
redirectTo: '/error/404'
});
The URL that I'm visiting for default page is:
This will fail and throw the error.
http://localhost:1969/en-US/user/someUserName
This will work fine
http://localhost:1969/user/someUserName
UPDATE
I figured out! This is the problem:
$locationProvider.html5Mode(true);
But why!?
I had the same error. In order to use html5Mode you need to make sure to set the <base href="" /> tag to the correct URL in your header. Check out the $location service docs for details.