Angularjs $routeprovider config using services - angularjs

My application is created by using MVC 4 + AngularJs.
In this application we are using Bundling for render the javaScript and css files, this is working as expected.
But for the $routeProvider we are using templateUrl to load the template in runtime, but the speed of page is decreased.
For the reason i have created the below folder structure:
HTML
Home
Index.html
Xyz.html
Student
Create.html
Update.html
I want to render these files using the bundling like below:
[
{
key:"/HTML/Home/Index.html",
val:"HTML text"
},
{
key:"/HTML/Home/Xyz.html",
val:"HTML text"
},
{
key:"/HTML/Student/Create.html",
val:"HTML text"
},
{
key:"/HTML/Student/Update.html",
val:"HTML text"
}
]
In that case we use $templatecache and caches all the key with values
and then we can able to use like below:
$routeProvider.
when('/Home', {
template: $templatecache.get("/HTML/Student/Update.html"),
controller: 'HomeController'
}).
when('/planning', {
template: '$templatecache.get("/HTML/Student/Create.html"),
controller: 'StudentController'
}).
otherwise({
redirectTo: '/Home'
});
This is taking long time in initial load, after that it loading fast.
How can we render template using bundling?
How to put html in $templatecache (using services or other)?

Related

How do i handle routing using both angular js and node js (express)?

I am creating single page application using node,express and angular js. I am not getting that how to manage routes in both angular and express(node).
Please help me and If possible give some example code or link.
If you wanna create a single page application you need to use the router of angular for navigate in your app without reload, but in generally you need data from server, to organise this data you need a route on node.
This is a example of ngRoute
http://plnkr.co/edit/L4Hxf7KiiVuouPWRv4pl
var app = angular.module("myApp", ["ngRoute"]);
app.config(function($routeProvider) {
$routeProvider
.when("/", {
template: 'hello'
})
.when("/red", {
template: 'hi'
})
.when("/green", {
templateUrl: 'hello.html'
})
.when("/blue", {
templateUrl: 'blue.html'
});
});
example of node route
http://expressjs.com/en/guide/routing.html

Angular will not use templatecache

Anguilar 1.5.*
I can not get angular to use my template cache. I gave it a invalid path name on purpose so I would get html file not found. But instead it is not using template cache and just requesting the html files.
templates.js
angular.module("gulpTemplates", []).run(["$templateCache", function($templateCache) { $templateCache.put("aaaa/views/examples.html"
...
app.js
var gulpNewy =
angular
.module('gulpNewy', ['ngRoute', 'gulpTemplates'])
.config(function($routeProvider) {
$routeProvider
.when('/home', {
templateUrl: 'views/home.html',
controller: 'homeCtrl'
})
.when('/examples', {
templateUrl: 'views/examples.html'
})
.when('/screen-shots', {
templateUrl: 'views/screen-shots.html'
})
index.html
<div class="col-xs-12" ng-view></div>
What am I doing wrong?
Ok, I found the issue.
If you are using ng-view, templateCache will not work by default for you. This is because with ng-view you do not need to use ng-include. templateCache needs to be used with ng-include or $templateCache.get.
Regardless of $templateCache.get, ng-view will not look in templateCache.

Angular-route can't find views

This is my angular app-file where i add my routing:
(function() {
var app = angular.module("CustCMS", ["ngRoute"]);
app.config(function ($routeProvider) {
$routeProvider
.when("/Index", {
templateUrl: "~/CustCMS/Views/User/Index.html",
controller: "IndexController"
})
.when("/User/:userid", {
templateUrl: "~/CustCMS/Views/User/User.html",
controller: "UserController"
})
.otherwise({ redirectTo: "/Index" });
});
}());
I have tried some different ways to enter the templateUrl but i always get this error:
[$compile:tpload] Failed to load template: ~/CustCMS/Views/User/Index.html (HTTP status: 404 Not Found)
What do i have to enter for the templateUrl to find my views?
So i found the answere. Angular can't handle cshtml files so you have to user static views. And becuase of some constraint in visual studio you can't have static views in the views folder so you have to create another folder and add the static views to that folder.
because you are including your index.html in some folder. change the otherwise route to
/CustCMS/Views/User/Index.html.

How to combine ng-view with complete pages in AngularJS?

I am developing an application that is supposed to be a single-page application.
The tools I am using are AngularJS, NodeJS, ExpressJS and Jade for templating.
So far I've been working with a page that has a ng-view directive on it, and I can change its content to display the page I want, while maintaining the side menu.
Now I've come to a point where I need to create a login/create account page (that I am calling 'intro', for now), and this one should use all the screen space, removing the menu as well.
How can I achieve this? My route file looks as follows:
var akaAcademicManagerApp = angular.module('akaAcademicManagerApp', ['ngRoute', 'akaAcademicControllers']);
akaAcademicManagerApp.config(['$routeProvider',
function($routeProvider) {
$routeProvider.
when('/dashboard', {
templateUrl: 'partials/dashboard',
controller: 'DashboardController'
}).
when('/profile', {
templateUrl: 'partials/profile',
controller: 'ProfileController'
}).
when('/intro', {
templateUrl: 'partials/introPage',
controller: 'IntroController'
}).
otherwise({
redirectTo: '/dashboard'
});
}]);
angular.module('akaAcademicControllers', []);
I think this can help you (yet, i don't think it's the optimal solution but it works)
in your index.html,after your body tag put this:
<div ng-include="accessToApplication()"></div>
Now in your controller:
$scope.loggedIn = false;// true when the user is logged in
$scope.accessToApplication = function () {
if (!$scope.loggedIn) {
return "partials/introPage.html";
}
else {
return "path to the page containing the ng-view";
}
};
I advice you to take a look at ui-router and multiple named views (https://github.com/angular-ui/ui-router/wiki/Multiple-Named-Views)

AngularJS : Load js file through UI router

I have AngularJs ui router in my application. I need to load js file based on my state.
.state('root.home',{
url: '/index.html',
views: {
'header': {
templateUrl: 'modules/header/html/header.html',
controller: 'headerController'
},
'content-area': {
templateUrl: 'modules/home/html/home.html',
controller: 'homeController'
},
'footer': {
templateUrl: 'modules/common/html/footer.html',
controller: 'footerController'
}
},
data: {
displayName: 'Home',
}
})
The headerController,homeController and footerController have different js files when the home state is loading at that time we need to load the controller js files Is it possible through the UI router?
I think you need to use a framework like RequireJs
it is loading your Script files base on the page you are showing to the user
RequireJS loads all code relative to a baseUrl.
check it. I think It might be helpful.
You should not load javascript files on the fly depending on your route status, instead you should concatenate all your js files and minify the compiled source via Gulp for instance.
https://www.npmjs.com/package/gulp-concat
https://www.npmjs.com/package/gulp-minify
ui-route is more powerful than requirejs (ng-route).
You may look her about differences:
What is the difference between angular-route and angular-ui-router?
http://www.amasik.com/angularjs-ngroute-vs-ui-router/

Resources