Angularjs routes are coming upto localhost only - javascript

I have to setup a wordpress site which was developed by another team locally. They used angularjs. I am very new to angular. I placed the wordpress files in wamp server. The name of the folder is playbook.
When I tried to access the site by using url localhost/playbook, I got a javascript error saying localhost/home not found.
I checked the javascript file and I saw routing like this
.when('/', {
controller: 'HomeCtrl',
templateUrl: '/home'
})
When I added /playbook at the start like below of templateUrl, page displayed
.when('/', {
controller: 'HomeCtrl',
templateUrl: '/playbook/home'
})
Why is this happening. Shouldn't the route take the path up to localhost/playbook?

You need to specify the extension .html for the template.Assuming your file is called home.html, replace your current code with
.when('/', {
controller: 'HomeCtrl',
templateUrl: '/playbook/home.html'
})
Regarding the usage of a more specific path.Consider this, you are coding a multi-module app in Angular.To seperate concerns, you may have a file structure that is based on each module.So my routes could look something like this. For my file, this was defined in app.js , which was inside the JS folder in my whole application.Additionally, you may choose to define routes in a seperate folder altogether. There is no reason for it to default to a particular file or folder.
foodApp.config(['$routeProvider',function($routeProvider){
$routeProvider.when('/',
{
templateUrl:'js/apps/choiceScreen/choice.html',
controller:'choiceCtrl'
})
.when('/cafe',
{
templateUrl:'js/apps/cafe/cafeScreen.html',
controller:'cafeCtrl'
})
.when('/showCafe/',
{
templateUrl:'js/apps/eachScreen/itemView.html',
controller:'itemCtrl'
})
.otherwise({
redirectTo: '/'
});
}]);

Related

How to create a layout template for angular routing?

I use angular routing:
angular.module("app-test", ["ngRoute"]).config(function ($routeProvider, $locationProvider) {
$locationProvider.hashPrefix('');
$routeProvider.when("/",
{
controller: "firstController",
templateUrl: "/views/first/index.html"
});
$routeProvider.when("/second",
{
controller: "secondController",
templateUrl: "/views/second/index.html"
});
$routeProvider.otherwise({ redirectTo: "/" });
});
But all my views of the different routes (e.g. views/first/index.html, views/second/index.html, etc.) have repeating html code like loading panels, messages, etc.
How can I outsource those common syntax in for example a layout html page that is used together with all the templateUrl views of the angular routing?
For example if every templateUrl has following html code - <div>{{message}}</div> - I would like to put this code in an extra html file which will be included in the templateUrl while the angular routing is getting the view.
You can use ng-include. But you can also use the same View (html) with different controllers:
$routeProvider.when("/",
{
controller: "firstController",
templateUrl: "/views/shared.html"
});
$routeProvider.when("/second",
{
controller: "secondController",
templateUrl: "/views/shared.html"
});
Please also note that a "views" folder doesn't scale very well. It works better to have a feature directory for each feature that contains all the pieces needed to build that feature. Shared stuff can get a bit dicey--if you know in advance you're going to share it, you can put it in a "shared" or "common" directory, but sometimes you wind up using it from the feature where it was first used.

angularjs nested routing doesn't load layout

.state('home', {
url: '/',
templateUrl: 'index.html',
abstract:true
})
.state('home.dashboard', {
url: '/dashboard',
templateUrl: 'dashboard/index.html',
controller: 'dashboardCtrl'
})
I failed to load index.html when I visit example.com/dashboard, I was only able to get the raw html of what is inside dashboard/index.html. That's strange, because in index.html I've declared <ui-view></ui-view> within the body so I expect dashboard/index.html to be a child of index.html.
Pushed a repo of my code.
You don't actually have nested states; you simply have a primary index.html file that serves as the container for your application, its dependencies, and your views. Angular won't load your primary index.html page, the index.html page loads Angular.
So there is no need for your 'home' state to have a templateUrl. You will use nested states if, for example, your dashboard view has an inner view that can load different templates depending on a user action. See Angular UI Router's documentation for some example use cases for nested views.
You can't declare index.html as your firststate like #jakeblues said.
Try to put all the specific content from index.html in a new template :
.state('home', {
url: '/',
templateUrl: 'home.html',
controller: 'homeCtrl'
})
.state('dashboard', {
url: '/dashboard',
templateUrl: 'dashboard/index.html',
controller: 'dashboardCtrl'
})
I downloaded your code from the repo.
The main problem I am seeing is that your angular code is not being loaded when you do example.com/dashboard
app.use(express.static(path.join(__dirname, 'public')));
This makes your public folder as the folder from which the project is being loaded.
When you do example.com, Your angular code is loaded because you are responded with public/index.html file which contains angular code and it is correct.
But when you do example.com/dashboard, it is actually loading html file /public/dashboard/index.html where there is no any angular code.
Try doing alert on dashboardCtrl.js. You will get nothing because it is not being loaded. It is only included in index.html file which is not loaded at all.
Try changeing url to dashboards and remove 'home.dashboard'
.state('dashboard', {
url: '/dashboards',
templateUrl: 'dashboard/index.html',
controller: 'dashboardCtrl'
})

Avoid all layouts being combined into one page?

I am trying to move over my bootstrap file on html to being implemented with meteor. I want everything to be on seperate pages, but my program is showing all my pages on one page even though I am routing the files via angular-ui-router. This is my routes.js file which I placed in the root/client folder:
angular.module("project").config(['$urlRouterProvider', '$stateProvider', '$locationProvider',
function($urlRouterProvider, $stateProvider, $locationProvider){
$locationProvider.html5Mode(true);
$stateProvider
.state('aboutus', {
url: '/aboutus',
templateUrl: 'client/views/about.html',
})
.state('contactus',{
url: '/contactus',
templateUrl: 'client/views/contact.html',
})
.state('home', {
url: '/home',
templateUrl: 'client/views/index.html',
})
.state('services', {
url: '/services',
templateUrl: 'client/views/services.html',
})
$urlRouterProvider.otherwise("/home");
}]);
If you want to see the whole project, here it is:
https://github.com/Aggr0vatE/project
From the docs (http://docs.meteor.com/#/full/structuringyourapp):
Meteor scans all the HTML files in your directory for three top-level elements: <head>, <body>, and <template>. The head and body sections are separately concatenated into a single head and body, which are transmitted to the client on initial page load.
You need to structure your html files to take advantage of Meteor templates. The fact that you have everything in head and body tags is why it's being concatenated together into one page. I'd suggest reading some of the docs in the above link.

Why doesn't root request get the updated index.html

I am developing an angular app. Whenever I modify the index.html, a request to localhost:8000 doesn't get the updated index.html. Why is that? I am using nodejs to host the server. I have tripped over this too many times assuming the request would get the updated index.html, such as including a new script but nothing happens because the index.html isn't the updated one. It is quite annoying every time I refresh I have to type index.html.
app.config(['$locationProvider', '$routeProvider', function ($locationProvider, $routeProvider) {
$locationProvider.html5Mode(true);
$routeProvider.when('/', {
redirectTo : '/login'
}).when('/dashboard', {
templateUrl: 'app/dashboard/dashboard.html',
controller: 'dashboard'
}).when('/login', {
templateUrl: 'app/layout/login.html'
}).when('/register', {
templateUrl: 'app/layout/register.html'
}).otherwise({
redirectTo: '/'
});
}]);
It's probably loading from the browser cache rather than the changed copy from the server.
Try
location.reload(true)
to load it from the server.
https://developer.mozilla.org/en-US/docs/Web/API/Location.reload

AngularJS relative url routing issue

I have my backend web framework loading my AngularJS app with following URL
http://localhost/New/Alpha/App
I also have it set up so that anything after App will still load the same thing
http://localhost/New/Alpha/App/home
http://localhost/New/Alpha/App/settings
...
I'm trying to make my AngularJS app to work in the way that it would pick up the bit of URL after App and load a controller/template accordingly. I have a problem with routing in my AngularJS app though
var main = angular.module("main", ["ui.bootstrap", "ngRoute"]);
main.config(function($routeProvider, $locationProvider) {
$routeProvider
.when("home", {
templateUrl: "assets/tpl/home.html",
controller: "mainController"
})
.otherwise({
redirectTo: "fail"
});
$locationProvider.html5Mode(true);
});
main.controller("mainController", function($scope) {
console.log("home")
});
If I try this URL
http://localhost/New/Alpha/App/home
it changes the URL to
http://localhost/fail
instead of leaving the URL as it is and loading the template/controller. If however I change the config and give it a full relative URL it does work as supposed to
.when("/New/Alpha/App/home", {
templateUrl: "assets/tpl/home.html",
controller: "mainController"
})
My problem is, that the part of URL before App - /New/Alpha cannot be hardcoded in. It could be /New/Beta, /New/Gamma, etc.
Is what I want to do possible at all without hardcoding the full relative URL match?
UPDATE Sorry, forgot to mention that the number of URL segments before App can change, as in it could be /New/Beta/App and it also could be /New/Another/Beta/App. I don't suppose something like */App or /New/*/App is possible instead of /New/:placeholder/App?
Will this work for you?
var main = angular.module("main", ["ui.bootstrap", "ngRoute"]);
main.config(function($routeProvider, $locationProvider) {
$routeProvider
.when("/New/:greek/App/home", {
templateUrl: "assets/tpl/home.html",
controller: "mainController"
})
.otherwise({
redirectTo: "fail"
});
$locationProvider.html5Mode(true);
});
main.controller("mainController", function($scope) {
console.log("home")
});
You could then retrieve the greek with $routeParams.greek from within your controller.
The general solution to this problem is to have the server pass the app URL to your client-side code. In other words, use server-side code to dynamically write the equivalent of the following on the page:
var appUrl = '/New/Alpha/App';
Then setting up the route provider becomes:
main.config(function($routeProvider, $locationProvider) {
$routeProvider
.when(appUrl + "/home", {
templateUrl: "/assets/tpl/home.html",
controller: "mainController"
})
.otherwise({
redirectTo: appUrl + "/fail"
});
$locationProvider.html5Mode(true);
});
That way the knowledge of the application base URL lives in one place — server-side (which makes sense as only the server is in a position to truly know, if you think about it).
In your specific case, if the application base URL is implicit in the URL structure, you could calculate the following client-side:
var appUrl = window.location.pathname.match(/^\/New\/.*\/App/);
Needs work, but you get the idea. Then you can set up the route provider exactly as above.

Categories

Resources