How to replace url part before # - angularjs

My angularJs App URL is opened via another application. URL to open my app looks like,
http://localhost:21770/App/IAnalytics?FromApp=abc#/PBIDefault
where FromApp is the parameter which indicates the source app from where angularjs app is called.
I would like to remove the FromApp=abc part from URL once my home page controller starts running.
I need to make sure by URL looks like,
http://localhost:21770/App/IAnalytics?#/PBIDefault
How can I achieve the same using $routeProvider or $location ?

You look for params once your home page controller starts .
If you find a parameter, you change the state of your app to the home page of the app (But without parameters this time). you can use $routeProvider, $location or $stateProvider for that.
Like this:
if ($location.search().FromApp){
$state.go('app.home',null, { reload: true }); // 'app.home' is name of your home page state.
}

Related

How to force start controller when click on just opened link

When i try to go to just opened link, $routeProvider doestn't start the controller again.
Example:
in html code I have some links(dynamically), for example url1, url2, url3.
when I click on url2, $routeProvider starts the controller.
after some time, If I click again on this link - controller doesn't get started (only works for different links)
How to force start the controller, when clicked on just opened link?
Or run init code for all links on click(but after this go to target link)?
Use $route.reload();
From the Docs:
$route.reload();
Causes $route service to reload the current route even if $location hasn't changed.
As a result of that, ngView creates new scope and reinstantiates the controller.
-- AngularJS $route API Reference - reload
Can you post your example? Based on how you have configured your $stateProvider might be the problem.

AngularJS Routing not working when url typed

So, I'm pretty new to AngularJS and I'm trying to use AngularJs ngRoute in my application.
It all works smoothly when I start at the application homepage:
http://localhost:8080/appName/
And when I click on links from this point it works smoothly.
However, when I type a URL that I know exists/works, it gives me a 404 error. If I go to that link by using the application instead of the url it loads fine, even though it has the same url.
Eg. http://localhost:8080/appName/search
will give a 404, even though that is the same url that is the default redirect.
Indeed, the only url that will load by typing in the location is the base URL I mentioned above.
My app.js looks like this:
app.config( ['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider){
$routeProvider
.when("/search", {
templateUrl: "search.html",
controller: "SearchController"
})
.when("/results", {
templateUrl: "results.html",
controller: "ResultsController"
})
.when("/browse", {
templateUrl: "browse.html",
controller: "BrowseController"
})
.otherwise({redirectTo:"/search"});
//This gets rid of the # on the urls to the templates
$locationProvider.html5Mode(true);
}]);
I am hosting this on a glassfish4 server.
Is there something obvious I am missing/misunderstanding about how ngRoute works? Is there some setting that I am missing?
All help appreciated...
EDIT 1: As #Matthew Green below says, I need to configure the webserver to return the index.html for all pages below
http://localhost:8080/appName
I know I am being completely dense here, but where abouts is this configured? I am hosting the code in a MAVEN Jersey-Quickstart-Webapp.
When you use ngRoute, you are using javascript to handle routing to create a SPA. That means you need to hit a real page that loads your routing for your application to know what page to route to.
For example, your http://localhost:8080/appName/ should be routing to your index.html which would contain the javascript for your routing. With that page loaded it knows how to handle the links you have in your application. However, if you were to go directly to http://localhost:8080/appName/pageName you also need that to load index.html, as it is the one that loads your routing. Once your routing is loaded it should direct you to the correct page in your application. Without redirecting in place, http://localhost:8080/appName/pageName is not a real page and therefore correctly returns a 404.
Knowing this, the thing you have to figure out is what kind of server setup you have to configure the appropriate redirects for everything under http://localhost:8080/appName/ to go to your index.html page.

Changing root URL Angular UI-Router

I'm learning Angular by rewriting a JS web application in Angular, and I'm having some struggles with replacing ngRoute with ui-router. Although I realise that it is designed to build SPAs, I'm only rewriting a branch of pages in angular.
My current understanding is that ui-routing requires you to define the root route '/'. Mine looks something like:
var memberPage = angular.module('memberPage', ['ui.router']);
memberPage.config(['$stateProvider', function($stateProvider){
$stateProvider
.state('userpage', {
url: '/',
template: '<h1>test</h1>',
controller: 'dashBoardController'
})
}]);
The problem - I think - is that the user never hits this route. The user logs in and is being redirected to /users/:id, and the page that is being rendered with this route loads the angular app.
The (sinatra) routing that happens on the server side before the angular app gets loaded:
get '/' do
redirect "/login"
end
get '/login' do
redirect_to_userpage_if_logged_in
erb :login
end
post '/login' do
redirect '/login' unless user_exists(params)
if correct_password(params)
set_session
redirect "/users/#{session[:user_id]}"
else
redirect '/login'
end
end
get '/users/:user_id' do |id|
#user = User.find(id)
erb :'users/index' # from this point I want to run my angular app
end
I was looking for a way to tell the ui-router that it should regard that route as the applications root route, by doing url: '/users/:id', but that doesn't work.
Is there way to achieve what I'm looking for?
Thanks in advance
EDIT:
My god, I forgot to put the basic ng-app='myapp' directive somewhere in my html, which explains why my initial state never became active. Having changed that, my initial configuration works perfectly fine.
I'm not sure whether to delete this question or to keep it here for educational purposes.
The defined routes are viable after the #. So the route defined as / will be accessible on /users/:id#/ or /users#/ or even /#/.

AngularJS routing with Mongoose webserver

I'm testing a website locally on my machine. It uses AngularJS for routing and page changes, and I'm attempting to test the routes using the Mongoose webserver (extremely light).
My code is as follows:
app.config(['$locationProvider', '$routeProvider', function ($locationProvider, $routeProvider) {
$locationProvider.html5Mode(true);
$routeProvider.when('/who', {templateUrl: '/js/partials/who', controller: 'whoPage'});
$routeProvider.when('/what', {templateUrl: 'partials/what'});
$routeProvider.when('/want', {templateUrl: 'partials/want'});
$routeProvider.otherwise({ redirectTo: '/' });
}]);
(I haven't set up controllers for some of the other pages yet. I've been testing the "who" page.)
I'm running the page from localhost:8080. In my application, when I click a link to change the location, nothing happens. The URL changes to "localhost:8080/who", but I get no messages from console, and I get no changes on my page. However, if I then refresh that URL, I get a 404 error.
I don't have any server-side routing set up. Is this a necessity for Angular apps? Is there something wrong with the code I've written, or should I try a different test webserver?
$locationProvider.html5Mode(true);
will make angular use "push state" from the HTML5 History API.
This means that you'll see the url change in the location bar, but that won't cause the browser to actually reload your page. When you reload the page, the browser will now fetch that url from the webserver, which doesn't have it.
A common trick is to use URL rewrites to map any url back to index.html. You should take care of not remapping the urls that point to static files such as your javascript and css resources. That's usually easy because it's a good practice to group all your css and js files in some directory instead of scattering them in the top level dir.
You can read about how to configure mongoose URL rewrites at https://www.cesanta.com/developer/binary#_url_rewrites

Angularjs ngRoute # in URL

This is my angularjs routing
app.config(function($routeProvider, $locationProvider) {
$routeProvider.
when('/home', {
templateUrl: 'partials/home.html'
}).
otherwise({
redirectTo: '/'
});
if (window.history && window.history.pushState) {
$locationProvider.html5Mode({
enabled: true,
requireBase: false
});
}
});
This link works as expected
Home
However, when I type http://localhost:3000/home directly in the address bar, I get a blank page. Any idea why this is happening?
Typing http://localhost:3000/#/home redirects to http://localhost:3000/home and works as expected.
AngularJS in html5Mode uses html5 history routing to get rid of the # in the url. This works as long as you navigate in the webpage.
The browser requested your index.html once (which set up your app and especially ngRoute), and clicking Home etc. does not cause requests to the server. Instead, ngRoute looks up the route, manipulates the html5 history and shows you what you clicked.
However, you have to tell the browser that performs the localhost:3000/home request someone typed into the url bar that he in fact wants index.html (not home/index.html, which does not exist). This means you have to configure your server to support html5 history routing. The $location documentation has a long post about this.
Here's what you'd add if you use express:
// this must be the last middleware added to your express app
app.use(function(req, res) {
res.sendFile('index.html'); // specify the path to your index.html here
});
If you type /home directly to the address bar, you're asking the server for a document in the /home path, when you actually want the AngularJS application in a document in the root path to load the /home route. You need to configure your server to redirect any request other than the root path to the root path, and pass the specified path after a #, for example, redirect /home to #/home.
Angular uses ngRoutes to navigate without leaving the page. If your route contains the # sign followed by the route then you are still on the same page. Angular does this so that it can have persistent variables between views. If you redirect to /home instead of #/home you are loading a static path to the /home folder on your server and Angular will reload when the new page loads, all variables will be cleared.

Resources