Multiple configuration providers with AngularJS - angularjs

How can I specify multiple providers to AngularJS?
Right now I'm doing it in 2 places, but this doesn't seem to work:
var myApp = angular.module("myApp", ['ngRoute']);
// configure our routes
myApp.config(function($routeProvider) {
$routeProvider
// route for the home page
.when('/', {
templateUrl : 'pages/home.html',
controller : 'mainController'
})
// route for the about page
.when('/about', {
templateUrl : 'pages/about.html',
controller : 'aboutController'
})
// route for the contact page
.when('/contact', {
templateUrl : 'pages/contact.html',
controller : 'contactController'
});
});
myApp.config(function($interpolateProvider){
$interpolateProvider.startSymbol('((').endSymbol('))');
}
);

Use a single function reference:
myApp.config(foo)
which injects each provider:
function foo($routeProvider, $interpolateProvider)
{
/* interpolateProvider configuration */
$interpolateProvider.startSymbol('((').endSymbol('))');
/* routeProvider configuration */
$routeProvider
.when('/', {
templateUrl : 'pages/home.html',
controller : 'mainController'
})
.when('/about', {
templateUrl : 'pages/about.html',
controller : 'aboutController'
})
.when('/contact', {
templateUrl : 'pages/contact.html',
controller : 'contactController'
});
}
/* module creation */
var myApp = angular.module("myApp",[]);
/* provider binding */
myApp.config(foo);
/* Manual bootstrapping */
angular.bootstrap(document, ['myApp']);
/* $inject Property Annotation */
foo.$inject['$routeProvider', '$interpolateProvider'];
Providers are essentially objects that are used to create and configure instances of AngularJS artefacts. Hence, in order to register a lazy controller, you would use the $controllerProvider. Similarly, To register a directive, you would use the $compileProvider, to register filters you would use the $filterProvider, and to register other services, you would use the $provide service.
References
AngularJS: Configuration Blocks
AngularJS: Inject Property Annotation

Related

Using $sce.trustAsResourceUrl in Angular routeProvider?

for my routing, I am using static templates that come from an external source. However, I keep getting the insecureUrl error from Angular. One solution apparently is to have it with the $sce.trustAsResourceUrl wrapped around it. Now, I am trying to do it the following way:
myapp.config(['$routeProvider', function($routeProvider, $sce) {
$routeProvider
.when('/', {
controller : 'ProductListController',
templateUrl : $sce.trustAsResourceUrl([my_external_template]),
reloadOnSearch: false
})
.otherwise({ redirectTo : '/' });
}]);
However, I get the injector error. Any help?
myapp.config(['$routeProvider', '$sce', function($routeProvider, $sce) {
$routeProvider
.when('/', {
controller : 'ProductListController',
templateUrl : $sce.trustAsResourceUrl([my_external_template]),
reloadOnSearch: false
})
.otherwise({ redirectTo : '/' });
}]);
Since you are manually doing minsafe syntax you need to add $sce to the string part of the injection array (ideally just don't do the minsafe syntax use a plugin to add that before uglification in your build process)

I am using ngRoute in my AngularJS project . Can I have specific action associated with each routes

This is how I am doing .Here is my code.
I need to have specific function for each route
var App = angular.module('App', ['ngRoute']);
App .config(function($routeProvider) {
$routeProvider
.when('/', {
templateUrl : 'pages/home.html',
controller : 'mainController'
})
.when('/about', {
templateUrl : 'pages/about.html',
controller : 'mainController'
})
.when('/contact', {
templateUrl : 'pages/contact.html',
controller : 'mainController'
});
});
I guess you want to use the 'resolve' function. Try like below
$routeProvider
.when('/', {
templateUrl : 'pages/home.html',
controller : 'mainController',
resolve: {
val: function() {
// do whats needed here
return true;
}
})
.when('/about', {
templateUrl : 'pages/about.html',
controller : 'mainController',
resolve: {
val: function() {
// do whats needed here
return true;
}
})

How to define a abstract in routeprovider?

I want to define a abstract in route provider as we can define it in a state provider to for parent.
.when('/home', {
title: 'home',
templateUrl : 'templates/home.html',
controller : 'HomeController'
})
.when('/about', {
title: 'about',
templateUrl : 'templates/about.html',
controller : 'AboutController'
})
i want to define a (/home) as abstract or is the any way to use a HomeController as a global for entire application.
My controller is :
.controller('HomeCtrl', function($scope, $http, oliveService, $location, $routeParams) {
console.log('home ctrl');
$scope.about_info = function(){
console.log('this is about info');
}
})
.controller('AboutCtrl', function($scope, $http, oliveService, $location, $routeParams) {
console.log('about ctrl');
$scope.about_info = function();
})
is the any way to use a HomeController as a global for entire application?
Yes,you can.
consider the index template :
HTML :
<body ng-view>
<div ng-controller="globalCtrl">
</div>
</body>
and define your controller as usual.
For the ngRoute router there are no abstract states like for uiRouter. Just attach a Controller to the Body Tag for example.

Passing variable name in angular route

Currently I am able to add routing mechanism in AngularJs with the following code :
App.config(function($routeProvider, $locationProvider) {
$routeProvider
.when('/', {
templateUrl : '/templates/home.html',
controller : 'landing'
})
.when('/login', {
templateUrl : '/templates/login.html',
controller : 'login'
});
});
What I need is an url in the format like /app/:appname which should fetch template called /template/:appname.html. I know that the appname can be accessed from controller using :
$routeParams.appname;
But how can I render template based on that parameter ?
You could do this:
App.config(function($routeProvider, $locationProvider) {
$routeProvider
.when('/app/:appname', {
templateUrl : '/templates/app-template.html',
controller : 'AppCtrl',
resolve: {
appPath: function ($route) {
return $route.current.params.appname;
}
}
})
});
in AppCtrl,
App.controller('AppCtrl', AppCtrl);
AppCtrl.$inject = [
'$scope'
'appPath'
];
function AppCtrl($scope, appPath) {
$scope.template = appPath + '.html';
}
in app-template.html, use ng-include as such
<div ng-include src="template"></div>
Hope this helps.
templateUrl can be use as function which returns a url.
App.config(function($routeProvider, $locationProvider) {
$routeProvider
.when('/', {
templateUrl : '/templates/home.html',
controller : 'landing'
})
.when('/:appname',{
templateUrl: function(params){
return "templates/"+params.appname +".html"
},
controller: 'Ctrl'
});
});

how to config the `router` in the `dev` environment?

Using my local host, i was worked my angular app. i moved this app to my dev environment, where i am working now.
But after i move to dev my router not working. i guess i need to do some configuration to work that. But i tried up to changing my routes but nothing works.
my new dev app url : http://azvsp14devapp01:123/_layouts/15/cpmd/public/ - in the public folder my index.html nested.
here is my app.js :
(function () {
"user strict";
angular.module("tcpApp", ["ngRoute","ngResource", "ngAnimate"])
.run(['$route', function ($route) {
$route.reload();
}])
.config(function ($routeProvider, $locationProvider) {
// $locationProvider.html5Mode(true);
$routeProvider
.when ("123/_layouts/15/cpmd/public/", { //trying for equalient of `/`
templateUrl : "views/login/login.html",
controller : "loginController",
className : "body-login"
});
$routeProvider
.when ("/lock", {
templateUrl : "views/lock/lockScreen.html",
controller : "lockScreenController",
className : "body-lockScreen"
});
$routeProvider
.when ("/home", {
templateUrl : "views/home/home.html",
controller : "homeController",
className : "body-home"
});
$routeProvider
.when ("/projectSummary/:id", {
templateUrl : "views/projectSummary/projectSummary.html",
controller : "projectSummaryController",
className : "body-projectSummary"
});
$routeProvider
.when ("/projectDetails", {
templateUrl : "views/projectDetails/projectDetails.html",
controller : "projectDetailsController",
className : "body-projectDetails"
});
$routeProvider
.otherwise ({
redirectTo:'123/_layouts/15/cpmd/public/'
});
})
})();
Change
"123/_layouts/15/cpmd/public/"
to
"/"
In both when and otherwise.
This because router place with index.html treats as root.

Resources