Index page separate from rest of AngularJS app? - javascript

I'm working on two projects right now using AngularJS, and I'm running into the same problem with both of them.
The problem is that I have an index page that looks completely different from any of the inner pages, which means that my ng-view has to consist of the entire page. This makes it so that any time a route changes, the whole page has to reload instead of just the main content area. This causes things like the header or sidebar to flash briefly.
The only good approach I can think of to make my index page separate from my app is to literally have a separate, static index.html and then all my angularJS pages inside a separate folder so that I can use a more focused ng-view.
Is this the only/best approach there is? Has anyone achieved this, or have any ideas on how to? thanks.

A way to solve this problem would be using UI-Router.
For example:
You could have an app.html which is a page that holds all of your application views. In it add a:
<body>
<div ui-view></div>
</body>
and styles/scripts required by the entire application.
All of your views will go there including the index.html view.
Assuming that the pages except the index have some sort of header/body/footer layout in which the body changes according to the actual page you can use a configuration as follows:
var app = angular.module('app', [])
.config(['$stateProvider', function ($stateProvider)
{
$stateProvider
.state('index', {
url: '/index',
templateUrl: 'index.html',
controller: 'IndexController'
})
.state('root', {
templateUrl: 'root.html',
controller: 'RootController'
})
.state('root.somePage', {
url: '/some-page',
templateUrl: 'some-page.html',
controller: 'SomePageController'
})
.state('root.anotherPage', {
url: '/another-page',
templateUrl: 'another-page.html',
controller: 'AnotherPageController'
});
}
The root.html will be like a masterpage in ASP.NET Webforms so it would be in the form:
<!-- header markup here -->
<div ui-view></div>
<!-- footer markup here -->

Related

Angular JS multi level routing

I have an application as following structure
Index.html
which has app.js with all routing and ng-view is added inside index.html
I have another template which am loding on successive login over this ng-view is home.html
can i have a ng-view inside the home.htmlas well ? and load to that ng-view when I click on any menus inside link of home page ?
I am adding my routing details bellow
.config(['$routeProvider', function ($routeProvider) {
$routeProvider
.when('/login', {
controller: 'LoginController',
templateUrl: 'modules/authentication/views/login.html'
})
.when('/home', {
controller: 'HomeController',
templateUrl: 'modules/home/views/home.html'
})
.otherwise({ redirectTo: '/login' });
}])
can I add a new routing and load a tempale and controler inside home.html in place of index.html ?
If I understood correctly you want a ng-view inside a ng-view. I think you cannot do that. But the solution would be use the parameter reloadOnSearch which you can set as false, so everytime you reload the page changing it's route it will not reload the previously loaded html structure.
.when('/home', {
controller: 'HomeController',
templateUrl: 'modules/home/views/home.html',
reloadOnSearch: false
})
But to make it work this way you have to type in the fixed html structure in every page you want. A way of doing that is adding includes to your home page and set the view pages by scope variables.
<!--home.html-->
<!--define your structure here-->
<!--includes that will load the desired pages-->
<div>
<div ng-include="'./indexGeneral.html'" ng-if="Page == 'general'"></div>
<div ng-include="'./indexContacts.html'" ng-if="Page == 'contacts'"></div>
<div ng-include="'./indexVehicles.html'" ng-if="Page == 'vehicles'"></div>
</div>

Pure CSS modal (::target) with angular ui-router

My first question here :)
I'm porting my site to Angular (I'm new to it) and wondering if I'd be able to keep my pure CSS modal dialogue box while using ui-router. In HTML it looks like this:
<a id="footerMail" href="#openContact"></a>
<div id="openContact" class="modal">
<div class="modal__container">
X
<form class="contact-form" action="/" enctype="application/x-www-form-urlencoded" method="post">
</form>
</div>
It uses ::target on openContact id. But when I'm trying to do that while using ui-router it, obviously, doesn't allow that path to happen since it's not defined as a state. How should I define it since I don't want to redirect to a new URL just open a modal that is overlay of the current state? My $stateProvider looks like this:
$urlRouterProvider.otherwise("/site/editor");
$stateProvider
.state('site', {
url: "/site",
abstract: true,
templateUrl: "partials/site.html",
controller: 'MainController as ctrl'
})
.state('site.director', {
url: "/director",
templateUrl: "partials/gallery.html",
controller: 'GalleryController'
})
.state('site.editor', {
url: "/editor",
templateUrl: "partials/gallery.html",
controller: 'GalleryController'
});
});
The link and modal code would ideally fire from the main 'site" state since it's common for all subpages. Or maybe that's too much work and I should rewrite it and not use CSS approach?
Thanks in advance!
If your modal is always present you should just be able to use ng-show to control when it's displayed.
I typically have an application level control that I have on my body tag and have it created with something like ng-controller=AppController as app. If you had a property of this controller named showModal that you initialize as false you can have your modal created outside of the div for your ui-view and have ng-show="app.ShowModal" then in any link where you want to display the modal in the function you call from its ng-click set app.showModal to false.
There are a lot of good approaches for modals in Angular though. I've been using mdDialog from Angular Material and this one: http://www.bennadel.com/blog/2806-creating-a-simple-modal-system-in-angularjs.htm is also good. I've used the one from UI Bootstrap as well.

How to avoid hardcoding URLs (or Routes) in AngularJS ? (provide URL resolution)

As I am in learning phase of AngularJS, today I noticed one thing about the tutorials avaliable for it.
In the tutorials, the URLs used are hardcoded in the HTML and JS files.
For example:
If I have my routes defined like this in my config:
// config.js
$routeProvider.when("/", { // Home Page
templateUrl: basePageUrl,
controller: "Home"
}).when("/searches", {
templateUrl: basePageUrl, // Search Page
controller: "SearchPage",
})
Now in my home.html , if I want to create a link to searches page. I have to do the following:
<!-- home.html -->
<div>
Hello Home
<p> Go to Searches </p> <!-- I had to hard code the link -->
</div>
So, I was looking for a solution so I don't have to hard-code the links in HTML or JS files.
More like, the urls resolution functionality that each Backend MVC framework provides.. e.g
Django: {% url %} templetag for HTML and reverse() for python code
Rails: <%= link_to %> tag for HTML etc..
Is there a URL resolution functionality provided by AngularJS or any plugin ?
Note: I can implement something similar by storing JS variables/object for urls, but that doesn't look a good design to me
You can use UI-Router for this. It has named 'states' instead of hardcoded urls
https://github.com/angular-ui/ui-router
In your case you could use:
myApp.config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('home', {
url: "/",
templateUrl: "partials/home.html",
controller: 'HomeController'
})
.state('searches', {
url: "/searches",
templateUrl: "partials/searches.html",
controller: 'SeachController'
});
And then reference it in your template as
<a ui-sref="searches">Searches</a>
This comes in handy when you want to handle more complicated urls
.state('book', {
url: '/books/:bookID/',
templateUrl: 'partials/book-detail.html',
controller: 'BookController'
})
Link in a template
<a ui-sref="book({ bookID: book.id })">{{ book.title }}</a>
You can then access bookID in your controller using $stateParams.bookID

What is the difference between an ng-controller directive and a controller in the route?

I worked through the tutorial on the AngularJS website and I noticed that in at step 7, they change how a controller is introduced into the application. Initially, they use a directive:
<body ng-controller="PhoneListCtrl">
...
</body>
However, it later gets changed to use a controller attribute as part of an ng-route.
$routeProvider.
when('/phones', {
templateUrl: 'partials/phone-list.html',
controller: 'PhoneListCtrl'
}).
/* rest of routes here */
Here's the git diff where the change is made. Is there a difference between these two techniques?
Controller using a ng-controller directive:
A new $scope is created on ng-controller element.
Explicit view-to-controller connection
Visible with inspect element, etc
Controller in a route:
A new $scope is created per route on the ng-view element.
The controller can request dependencies defined in the route resolve.
Optional view-to-controller connection. Recommended to have a naming convention that maps routes to controllers to views.
One of well-known feature of Angularjs is Single-Page Applications.
If you assign ng-controller attribute directly on the page:
<body ng-controller="PhoneListCtrl">
...
</body>
you can't switch controllers easily for other tasks.
So, use route to switch controllers is one of important step in learning Angular Single-Page feature.
You can have same layout and one different element by using route and ng-view directive.
$routeProvider.
when('/phones', {
templateUrl: 'partials/phone-list.html',
controller: 'PhoneListCtrl'
}).
when('/tablets', {
templateUrl: 'partials/tablet-list.html',
controller: 'TabletListCtrl'
}).
If '/phones'
<div ng-view></div>
will include your 'partials/phone-list.html' template
and set 'PhoneListCtrl' as div controller
The same:
If '/tablets'
<div ng-view></div>
will include your 'partials/tablet-list.html' template
and set 'TabletListCtrl' as div controller
This is the difference between two.
ng-view is the cause of the difference. You can't really do this
<div ng-view ng-controller="PhoneListCtrl">
As you'd need to change that controller as the route changed. So basically the router does that for you, and uses the controller you specified when you defined your routes.
You probably can do this:
<div ng-view>
and then in your template:
<div ng-controller="PhoneListCtrl">
and leave out the controller declaration in your routes. Which I suspect would have essentially the same effect, although I've never tried that. Probably better to go with convention here though.
In the 1st case the controller is directly on the page.
Once they change it, that controller is only on the page if the route is /phones otherwise it is some other controller based on some other route.
Yes - the change is this:
if you want to display a specific controller on the page, you can use
<body ng-controller>
BUT
if you want to do routing (application with more than one controller) - you will need to use routing + change the body to:
<body ng-view></body>

How structure my angularjs app

I don't know how to "include" partials on templates. For example, I've an template "index". When the user navigate to "/" I like re render some divs on "index" from partials (login box and some others text div), when de user navigate to "/newAccount" I like re render same divs on "index" but with others partials.
The question is, it's the correcly way to use of AngularJS? Similar to the includes on Facelets?
Thanks! ;)
Here a pseudo code of my idea:
Template:
<html>
<body>....{include:mainBox}....{include:foot}...</body>
</html>
Partials for /newAccount:
{define mainBox}
<div>New user</div>
<div>Username:<input/></div>
{define foot}
<div>...</div>
Update:
Some like this is wrong?
function IndexCtrl($scope) {
$scope.mainPage = 'partials/index/loginbox.html',
$scope.footPage = 'partials/index/footer.html',
}
After in the template I put:
<div ng-include="mainPage"></div>
...
<div ng-include="footPage"></div>
And the route:
when('/', {
templateUrl: 'partials/index/layout.html',
controller: IndexCtrl
})
Use ng-include for this. Maybe what you want is ng-view + $routeProvider

Categories