This link does not work in all browsers in my angularjs onepage app.
homepage
Depending on the browser the url becomes different. In some browser the url ends with a trailing "/#" and others with "/#/" e.g. "http://localhost/#" and "http://localhost/#/". This means that in non IE browsers request ending with /# fails to load ViewA
This is my route provider:
mainApp.config(["$routeProvider",
function ($routeProvider) {
$routeProvider
.when("/", {
templateUrl: "Views/ViewA.html"
})
.when("/home", {
templateUrl: "Views/ViewB.html"
})
.otherwise({
redirectTo: "/"
});
}]);
Is there a way to make the browsers behave the same? A library to reference?
My only references er:
angular.js
angular-route.js
I suggest you to take a look at the answer to a similar question here
It basically depends on the angular routing mode you want to use:
Hashbang Mode;
HTML5 Mode;
Hashbang in HTML5 Mode
I'd say your anchor should be:
homepage
Adding a locationProvider with html5mode set to true, seems to solve my issues
mainApp.config(["$locationProvider", "$routeProvider",
function ($locationProvider, $routeProvider) {
$locationProvider.html5Mode(true);
$routeProvider
.when("/", {
templateUrl: "Views/ViewA.html"
})
.when("/home", {
templateUrl: "Views/ViewB.html"
})
.otherwise({
redirectTo: "/"
});
}]);
Related
I can't properly whitelist urls.
I'm getting "Error: [$sce:insecurl]".
The paths exist, and when I place the templates in the same folder it all works.
What is the problem?
var platform = angular.module('platform', ['ngRoute', 'testControllers', 'testServices']);
platform.config(['$sceDelegateProvider', '$routeProvider',
function($sceDelegateProvider, $routeProvider) {
//$sceDelegateProvider.resourceUrlWhitelist(['self', '../templates/**']);
$sceDelegateProvider.resourceUrlWhitelist(['self', 'C:/Users/Royi/Desktop/Platform/templates/**']);
$routeProvider.
when('/', {
templateUrl: 'C:/Users/Royi/Desktop/Platform/templates/text.html',
controller: 'testController'
}).
when('/:pageId', {
templateUrl: 'C:/Users/Royi/Desktop/Platform/templates/text.html',
controller: 'testController'
}).
otherwise({
redirectTo: '/'
});
}]);
C:/ is not usually an URL accessible over HTTP. The page may pick up items in the local directory, but for using C:/ you should usually use file:///C:/ (note the third slash).
Also, as paths on your own drive can't be "trusted" by something like Chrome, you'll be getting issues. See the documentation for insecurl enter link description here
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 have an SPA anularjs app with a few pages.
Router Provider is configured as follows:
$locationProvider.html5Mode(true);
var path = 'web/partials/views/';
$routeProvider
.when('/', {
templateUrl: path + 'home.html',
//controller: 'mainController'
})
.when('/benefits', {
templateUrl: path + 'benefits.html',
//controller: 'aboutController'
})
.when('/gallery', {
templateUrl: path + 'gallery.html',
//controller: 'contactController'
})
.when('/quote', {
templateUrl: path + 'quote.html',
//controller: 'contactController'
});
This works - think nav links on index.html work and the address bar gets the correct url (myapp.com/benefits, myapp.com/quote, etc.)
What doesn't work is going directly to myapp.com/quote or myapp.com/benefits or other pages. I get "resource does't exists" - which makes sense - resource really doesn't exist.
My question is what is the appropriate way to handle this i AngularJS?
Thanks!
There are error handlers (like $routeChangeError) which you could utilize to redirect to a default page, check this out:
angular route service
i got this problem with angular when I open the site in Chrome
_localhost/angular/_
the url changes to
_localhost/#_
however in firefox everything is as expected
I asume its caused by the routeProvider
var Angapp = angular.module('angApp', []).
config(['$routeProvider', function ($routeProvider) {
$routeProvider.when('/', {
templateUrl: 'partials/main.html',
controller: mainCtrl
})
.when('/:catId', {
templateUrl: 'partials/category.html',
controller: categoryCtrl
})
.when('/detail/:detId', {
templateUrl: 'partials/detail.html',
controller: detailsCtrl
})
.otherwise({
redirectTo: '/'
});
}]);
I hope someone can help me with this and explain what is happening what did i miss.
thanks in advance.
EDIT:
when I add
.....
config(['$routeProvider','$locationProvider', function($routeProvider. $locationProvider){
.......
$locationProvider.html5Mode(true);
the routing breaks completely in Chrome and firefox
_localhost/angular/#%2Flink_
when i remove the "#%2F" and reload the page I get a 404 as return
when i hit backspace then the site loads correctly!
Please be sure that your browser supports HTML5. For browsers that don't support HTML5 history API AngularJS will automatically fall back to hasbang URLs so no way to "remove" the hash(#).
I'm working on a web page that is using Angular, jQuery Mobile, and the jQuery Mobile Angular adapter by tigbro. I have everything up an running and it works great except for one issue and that is if at any point if you refresh the page using the browser's refresh button it will give a 404 error as if it doesn't understand the route anymore. I'm not sure where the issue might be since it gets a little confusing with the two frameworks and the adapter working together and I'm new to all of these technologies.
IE happens to be the only browser this doesn't happen in and the difference seems to be in the url. Here is what it looks like when you browse to a page in IE:
http://site.com/SalesManagement/SalesManagementIndex.aspx#!/ViewNotes/4
Here is what it looks like when you browse to the same page in another browser like Chrome:
http://site.com/SalesManagement/ViewNotes/4
If you go to the first url in Chrome it will load the page and then rewrite the url to the 2nd one.
Below is my routing configuration:
var SalesManagementApp = angular.module('SalesManagementApp', [])
SalesManagementApp.config(['$routeProvider', '$compileProvider', function ($routeProvider, $compileProvider) {
$routeProvider
.when('/Search', { templateUrl: 'Views/GrowerSearchView.aspx' })
.when('/SearchResults', { templateUrl: 'Views/GrowerResultsView.aspx' })
.when('/ViewNotes/:growerIndexKey', { templateUrl: 'Views/NotesHistoryView.aspx' })
.when('/EditNote/:growerIndexKey/:noteIndexKey', { templateUrl: 'Views/UpsertNoteView.aspx' })
.when('/AddNote/:growerIndexKey', { templateUrl: 'Views/UpsertNoteView.aspx' })
.when('/', { templateUrl: 'Views/GrowerSearchView.aspx' })
.otherwise({ templateUrl: 'Views/GrowerSearchView.aspx' });
} ]);
I've read some about html5 mode verse hashbang mode but setting html5 mode to off or on in the configuration just made my routing not work at all. Any help would be much appreciated.
I figured this out thanks to a similar question on the github site for the adapter: https://github.com/opitzconsulting/jquery-mobile-angular-adapter/issues/163
The fix for this is to disable html5Mode in angular and prefix your links with the # character. This makes your urls a little uglier as you are no longer using the html5 history API but in my case that doesn't matter. Optionally you can specify a hash prefix (by default it seems to be !) but I set mine to empty string. I couldn't find any documentation telling me why this is useful but its important to know what the prefix is so you can properly set your links.
Below is my updated routing configuration. Notice I now inject the $locationProvider.
var SalesManagementApp = angular.module('SalesManagementApp', [])
SalesManagementApp.config(['$routeProvider', '$compileProvider', '$locationProvider', function ($routeProvider, $compileProvider, $locationProvider) {
$locationProvider.html5Mode(false).hashPrefix("");
$routeProvider
.when('/Search', { templateUrl: 'Views/GrowerSearchView.aspx' })
.when('/SearchResults', { templateUrl: 'Views/GrowerResultsView.aspx' })
.when('/ViewNotes/:growerIndexKey', { templateUrl: 'Views/NotesHistoryView.aspx' })
.when('/EditNote/:growerIndexKey/:noteIndexKey', { templateUrl: 'Views/UpsertNoteView.aspx' })
.when('/AddNote/:growerIndexKey', { templateUrl: 'Views/UpsertNoteView.aspx' })
.when('/', { templateUrl: 'Views/GrowerSearchView.aspx' })
.otherwise({ templateUrl: 'Views/GrowerSearchView.aspx' }); // jQuery Mobile seems to ignore the / and just use .otherwise.
$compileProvider.urlSanitizationWhitelist(/^\s*(https?|ftp|mailto|file|tel):/);
if (!localStorage.SessionInfo)
window.location = '/Login.aspx';
} ]);
My links now look like: #/ViewNotes/{{growerIndexKey}}