angular-route doesn't seem to activate automatically - javascript

I'm currently having an issue with the angular-route#1.5.8 module.
The router does not seem to activate on its own. What I mean is that when I call the page (http://localhost/index.html), the requested template does not show up. However, manually adding a hash (#) to the end of the url, activates the router and shows the correct template.
I build a test application with nearly the same router-configuration, and in that app the router works as expected.
There are no errors logged in the console.
This is the config:
angular.module('app', ['ngRoute'])
.config(function ($routeProvider) {
$routeProvider
.when('/', {
templateUrl: './app/html/default.html'
})
.when('/login', {
templateUrl: './app/html/login.html',
})
.otherwise({
redirectTo: '/'
});
});
The templates are basically a span-tag with some text to show what template is being displayed.
Any help is appreaciated.

Did you put ng-view directive on your index page? You should share more of your the relevant routing codes, your html, your templates.

Related

Angularjs Refresh page (f5) cancel routing

I have this routing on my webpage:
app.config(function($routeProvider)
{
$routeProvider
.when("/",
{
//
})
.when("/DokumentRoute",
{
templateUrl : "static/routes/dokument.html",
controller : "dokumentController"
})
.when("/MarkningarRoute",
{
templateUrl : "static/routes/markning.html",
controller: "markningarController"
})
.otherwise(
{
templateUrl: "static/routes/pageNotFound.html",
controller : "pageNotFoundController"
});
});
When I refresh the page (F5) I want to cancel the routing, meaning I don't want to show anything within the ng-view tag
<div ng-view id="view"></div>
This is what happens now:
When I start the web page it shows no routing-page. I click on "dokument" and it routes to the dokument page, which shows in the ng-view tag.
When I hit F5 the page refreshes, but it STILL SHOWS the dokument route!
This is what I want:
I want the routing to cleared. I want the web site to look like when I first entered it. (nothing showing in the ng-view tag)
How can I fix this?
If anyone is interested, this is how I solved it:
app.run(function ($location, $rootScope) {
$rootScope.$on('$routeChangeStart', function (event, next, current) {
if (!current) {
$location.path('/');
}
});
});

Angular UI Router redirecting to default page

I have a modular application where i have routes split into multiple files.
So my system is.
Top
--Main.Module.js
--Main.Routes.js
--Main.Controllers.js
--Main.html
--user (folder)
----User.Module.js
----User.Routes.js
----User.Controllers.js
And in the user folder I have a login folder with a Login.html and a register folder with a Register.html
The main module file looks like this
angular.module('Main', ['ionic', 'Main.Routes', 'Main.Controllers', "User"])
.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
if (window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
cordova.plugins.Keyboard.disableScroll(true);
}
if (window.StatusBar) {
// org.apache.cordova.statusbar required
StatusBar.styleDefault();
}
});
});
The main route file contains
angular.module('Main.Routes', [])
.config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('Loader', {
url: '/',
templateUrl: '/views/Main.html',
controller: 'MainController'
});
$urlRouterProvider.otherwise('/');
});
And the Main.Module.js injects these two things as well as the User module which js file looks like this
angular.module('User', ['User.Controllers', 'User.Services', 'User.Routes']);
The user routes file has the routes for the login page and the register page
angular.module('User.Routes', [])
.config(function($stateProvider) {
$stateProvider
.state('Login', {
url: '/user/login',
templateUrl: 'views/user/login/Login.html',
controller: 'LoginController'
})
.state('Register', {
url: '/user/register',
templateUrl: 'views/user/register/Register.html'
});
// if none of the above states are matched, use this as the fallback
});
And when I do $state.go("Login") from the main controller it takes me to the login page without any issue. But I have a register button on the login page which is associated with this block of code
//Move user to the register page
$scope.registerClick = function(){
$state.go("Register");
}
Which redirects me to the register page for around half a second then immediately kick it back over to the main html page. So my question is i need to know why the state isnt staying with the register page and is moving immediately to the main.html page. The register page is part of the page history stack because i can go "back" to it with either the hardware back button or pressing back on chrome during testing. I tried moving the routes back to the main route file and not injecting the user routes but it produced the same results.
==============EDIT=============
Once the application loads, if i change the page it goes to after the main page to the register page and add a link back to the login page this behavior does not occur. I'm just confused on whats different.
set an otherwise
.config(function($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('/defaultpage');
.state('login', {
url: '/login',
templateUrl: 'templates/template.html',
controller: 'Ctrl'
})
.otherstates etc....
});
I see what I did wrong. In the location where I had my button that was supposed to take me to the register page alongside the ng-click directive i also had a href directive set to #. Thank you all for your input though.
The offending line
Create an account
What it should have been
Create an account

Can I use href with urlRouterProvider?

In my angularjs app I have replaced ngRoute (routeProvider) with ui.router (urlRouterProvider) module and stateProvider to navigate between states within my app.
Now I find out that ui-router only works with states and <a ui-sref=""> instead of <a href="">. As I have to set the application navigation outside of my app by using javascript, I can not change the anchors from href to sref. Here's how I set navigation links.
Portal.setNavigation(
[
{
"label": "Application1",
"selected": true,
"url": "/web/guest#/Application1"
}
]
);
This code set's the href attributes accordingly. Here is my stateProvider snippet, which works on manually placed srefs like this one: <a ui-sref="Application1">Application State 1</a>
app.config(['$stateProvider', '$urlRouterProvider',
function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('Application1', {
url: '/Application1',
templateUrl: '/Application.html',
controller: 'AppCtrl'
});
}]);
Any ideas how I can continue using ui-router AND using hrefs to navigate through states?
If you are navigating outside of your application through javascript by doing something like window.location(url), you should be able to to put your state's route in the url and the router should be able to handle the route i.e. window.location(/state1).
Here's the text from the ui-router that sheds some light on this (link):
Here's how you set a basic url.
$stateProvider
.state('contacts', {
url: "/contacts",
templateUrl: 'contacts.html'
})
Now when the user accesses index.html/contacts then the 'contacts'
state would become active and the main ui-view will be populated with
the 'contacts.html' partial.

Page automatically refreshing on AngularJS for some reason

In my app, the page periodically refreshes itself whenever I click a link that loads a different partial via the $routeprovider. Then, in addition to refreshing, it takes me back to the default partial (i.e., I'd have to click the link again in order to get where I wanted to go). I'm not using any methods that refresh the page at all; why might this be happening?
Also, interestingly enough, this doesn't happen when I click any non-$routeprovider links. The following is my routeProvider:
.config(['$routeProvider', '$locationProvider',
function($routeProvider, $locationProvider) {
$routeProvider.
when('/', {
templateUrl: "index.html"
}).
when('/profile/:id', {
templateUrl: "profile.html",
controller: 'UsersController as profiledUser'
}).
when('/gift/:id', {
templateUrl: "gift.html",
controller: 'GiftsController as profiledGift'
}).
when('/users-form', {
templateUrl: "users-form.html",
controller: 'UsersController as newUser'
}).
when('/login-form', {
templateUrl: "login-form.html",
controller: 'UsersController as loginUser'
}).
when('/gifts-form', {
templateUrl: "gift-form.html",
controller: 'GiftsController as newGift'
}).
when('/about', {
templateUrl: "about.html"
}).
otherwise({
redirectTo: '/'
});
Here's an example of one of the links, which links to the currently logged in user's profile, when inspected in Chrome's dev tool:
Profile
Here's that same link in the code, where it's part of a navbar:
<li>Profile</li>
To reiterate, clicking any of the above links occasionally both redirects to .otherwise and refreshes the page. I have not been able to reliably reproduce this behavior, as it seems to happen randomly; strangely enough, it happens even if I remove the .otherwise route from the config.
It looks like you are trying to do 2 things at once with the click on the <a> tag but only 1 is happening:
The default action, which is in the href and says "load UsersController + profile view" and
set template=1 in the current context
Even if template=1 has occurred, once you switch to the UsersController you no longer have access to this setting.
Try something like this:
Profile
plus
.when('/profile/:id/:template', {
controller: 'UsersController as profiledUser',
templateUrl: 'profile.html'
})
Or, use $rootScope or some other global context for the template=1 so it's always available to any controller+view as long as you've dependency injected it.

Angularjs deep linking to visual content (like modals)

My application has several 'modal' windows, For now there is no specific route to reach an open modal directly. I mean, written the url directly in the browser.
There are a Jquery solution, but how implement some similar solution for angular? where placed? when should run?
You can perform this sort of task within the routing config of your app.
For example, this one is using ui-router, for the routes, and ui-bootstrap for the modals.
In the route config add an onEnter which will fire when the route is first entered.
.state('login', {
onEnter: function ($stateParams, $state, $modal) {
$modal.open({
keyboard: false, // prevents escape-key closing modal
backdrop: 'static', // prevents closing modal outside of the modal
templateUrl: '/views/login', // view to load
controller: 'LoginCtrl' // controller to handle
})
}
})
Now, when navigating to the, in this example, login page the route will open the modal for me.

Categories