AngularJS Configuration Structure - angularjs

I want to summarize all module configurations within a block and it doesn't work and I don't get any error message.
The following works:
angular.module('myApp', [
'ngRoute',
'oc.lazyLoad',
'myApp.login',
]).
config(
[
'$routeProvider', function($routeProvider) {
$routeProvider
.otherwise({redirectTo: '/login'});
}
]
);
angular.module('myApp').config(
function($ocLazyLoadProvider) {
$ocLazyLoadProvider.config(
{
modules: [{
name: 'icons_and_fonts',
files: [
'components/font-awesome/css/font-awesome.min.css',
'components/weather-icons/css/weather-icons.min.css'
]
}],
debug: true,
events: true
});
}
);
But if I want to integrate $ocLazyProvider into the configuration block above, it doesn't work, the following code does not work:
config(
[
'$routeProvider', function($routeProvider) {
$routeProvider.otherwise({redirectTo: '/login'});
}
],
[
'$ocLazyLoadProvider', function($ocLazyLoadProvider) {
$ocLazyLoadProvider.config(
{
modules: [{
name: 'icons_and_fonts',
files: [
'components/font-awesome/css/font-awesome.min.css',
'components/weather-icons/css/weather-icons.min.css'
]
}],
debug: true,
events: true
}
)
}
]
);
What could be the problem?

You should pass a single array/function into the config block, instead of multiple. you can perform both configurations in a single function, that would look something like:
config(['$routeProvider','$ocLazyLoadProvider',function($routeProvider,$ocLazyLoadProvider) {
$routeProvider.otherwise({redirectTo: '/login'});
$ocLazyLoadProvider.config(
{
modules: [{
name: 'icons_and_fonts',
files: [
'components/font-awesome/css/font-awesome.min.css',
'components/weather-icons/css/weather-icons.min.css'
]
}],
debug: true,
events: true
}
)
}
]
);

Related

AngularJS lazyload routing is not working properly

Can someone help me with this routing? It always gives the following error:
Uncaught Error: [$injector:modulerr]
The code in question:
var app = angular.module('mainApp', ['ui.router', 'oc.lazyLoad','ngRoute', 'commonApp', 'adminApp', 'ui.bootstrap', 'gm', 'ngMessages', 'daterangepicker','customDirectives', 'mw-datepicker-range','uiGmapgoogle-maps','ngAutocomplete','ngImageCompress']);
app.config(['$ocLazyLoadProvider', '$stateProvider', '$urlRouterProvider','uiGmapGoogleMapApiProvider' , function($ocLazyLoadProvider, $stateProvider, $urlRouterProvider,uiGmapGoogleMapApiProvider) {
$urlRouterProvider.otherwise("/home");
//Config For ocLazyLoading
$ocLazyLoadProvider.config({
'debug': true, // For debugging 'true/false'
'events': true, // For Event 'true/false'
'modules': [{ // Set modules initially
name : 'home', // State1 module
files: ['home/home.controller.js',
'home/home.service.js',
'home/homeMapper.service.js',
]
},{
name : 'about', // State2 module
files: ['about/about.controller.js']
}]
});
//Config/states of UI Router
$stateProvider
.state('home', {
url: "/home",
views : {
"" : {
templateUrl:"/home/home.html"
}
},
resolve: {
loadMyCtrl: ['$ocLazyLoad', function($ocLazyLoad) {
return $ocLazyLoad.load('home'); // Resolve promise and load before view
}]
}
})
.state('about', {
url: "/about",
views : {
"" : {
templateUrl:"/about/about.html"
}
},
resolve: {
loadMyCtrl: ['$ocLazyLoad', function($ocLazyLoad) {
return $ocLazyLoad.load('about'); // Resolve promise and load before view
}]
}
});
}]);

loading modules and factories files using angular ui routing and ocLazyLoad

i', using angular ui routing with ocLazyLoad to load the appendices files according to the choosen stat as the following code shows
my problem is:
when i load a new state and click refresh sometime the factories is not initialized -i think it's because the files is not fully loaded before init the controller-
i also tried to merge all files in the same ocLazyLoad function and use serie : true but dosenot work
is it the right use of ocLazyLoad
i've the following modules
angular.module('app', [ "oc.lazyLoad"]);
angular.module("app.inventory", []);
angular.module("app.sales", []);
and here is the routing
.state("invoicesAddEdit", {
url: "/invoice/:invoiceId",
templateUrl: "app/components/sales/invoice/views/invoiceAddEdit.view.html",
controller: "InvoiceAddEditController",
resolve: {
invoiceId: ['$stateParams', function ($stateParams) {
return $stateParams.invoiceId;
}],
settings: ['$ocLazyLoad', function ($ocLazyLoad) {
return $ocLazyLoad.load({
name: "app.settings",
files: [
"app/components/settings/settings.module.js",
"app/components/settings/currency/services/currency.factory.js",
"app/components/settings/deliveryMan/services/deliveryMan.factory.js",
]
})
}],
inventory: ['$ocLazyLoad', function ($ocLazyLoad) {
return $ocLazyLoad.load({
name: "app.inventory",
files: [
"app/components/inventory/inventory.module.js",
"app/components/inventory/customer/services/customer.factory.js",
"app/components/inventory/store/services/store.factory.js",
"app/components/inventory/product/services/product.factory.js",
]
})
}],
purchasing: ['$ocLazyLoad', function ($ocLazyLoad) {
return $ocLazyLoad.load({
name: "app.purchasing",
files: [
"app/components/purchasing/purchasing.module.js",
"app/components/purchasing/purchaseOrder/services/purchaseOrder.factory.js",
]
})
}],
sales: ['$ocLazyLoad', function ($ocLazyLoad) {
return $ocLazyLoad.load({
name: "app.sales",
files: [
"app/components/sales/sales.module.js",
"app/components/sales/representative/services/representative.factory.js",
"app/components/sales/invoice/services/invoice.factory.js",
"app/components/sales/invoice/controllers/invoiceAddEdit.controller.js",
]
})
}],
}
})
Try to inject the oCLazy Load as deps and insert the required files before specific tag in the html like the following :
resolve: {
invoiceId: ['$stateParams', function ($stateParams) {
return $stateParams.invoiceId;
}],
deps: ['$ocLazyLoad', function ($ocLazyLoad) {
return $ocLazyLoad.load({
name: "app.settings",
insertBefore: '#ng_load_plugins_before',
files: [
"app/components/settings/settings.module.js",
"app/components/settings/currency/services/currency.factory.js",
"app/components/settings/deliveryMan/services/deliveryMan.factory.js",
]
})
}],
Add the following link to the header of the html page or the View:
<link id="ng_load_plugins_before"/>

Explain why this doesn't work? Removing dependency in angular breaks app

I've been working on some boilerplate stuff and ran into something peculiar.
I have a module and it works fine:
define([
'angular',
'angular-couch-potato',
'angular-sanitize',
'angular-ui-router',
], function (ng, couchPotato) {
"use strict";
var module = ng.module('app.home', [
'ngSanitize',
'ui.router'
]);
module.config(function ($stateProvider, $couchPotatoProvider) {
$stateProvider
.state('app.home', {
url: '/',
views: {
"content#app": {
controller: 'HomeCtrl',
templateUrl: 'app/components/home/views/home.html',
resolve: {
deps: $couchPotatoProvider.resolveDependencies([
'components/home/controllers/HomeController'
])
}
}
},
data:{
title: 'Home'
}
});
});
couchPotato.configureApp(module);
module.run(function($couchPotato){
module.lazy = $couchPotato;
});
return module;
});
Now if I remove angular-sanitize from define like so:
define([
'angular',
'angular-couch-potato',
'angular-ui-router',
], function (ng, couchPotato) {
"use strict";
var module = ng.module('app.home', [
'ui.router'
]);
module.config(function ($stateProvider, $couchPotatoProvider) {
$stateProvider
.state('app.home', {
url: '/',
views: {
"content#app": {
controller: 'HomeCtrl',
templateUrl: 'app/components/home/views/home.html',
resolve: {
deps: $couchPotatoProvider.resolveDependencies([
'components/home/controllers/HomeController'
])
}
}
},
data:{
title: 'Home'
}
});
});
couchPotato.configureApp(module);
module.run(function($couchPotato){
module.lazy = $couchPotato;
});
return module;
});
It no longer works. Chrome throws this error:
Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/1.2.26/$injector/modulerr?p0=app&p1=Error%3A%20…gleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.2.26%2Fangular.min.js%3A18%3A170)
My controller is empty with it's only dependency $scope and the view file just has hello world around p tags.
Can someone please explain to me what I am missing or why this is happening?

Unknown provider: ngMessagesProvider with require.js

I am trying to use ngMessages in my controller:
I am setting up my require.js config:
require({
// libraries dependencies (fallback support)
paths: {
jquery: [
'vendor/jquery/2.1.3/jquery.min'
],
bootstrap: [
'vendor/bootstrap/3.3.2/js/bootstrap.min'
],
angular: [
'vendor/angular.js/1.3.11/angular.min'
],
angularResource: [
'vendor/angular.js/1.3.11/angular-resource.min'
],
angularAnimate: [
'vendor/angular.js/1.3.11/angular-animate.min'
],
ngMessages: [
'vendor/angular.js/1.3.11/angular-messages.min'
],
uiBootstrap: [
'vendor/angular-ui/bootstrap/0.12.0/ui-bootstrap-tpls.min'
],
uiRouter: [
'vendor/angular-ui/ui-router/0.2.13/angular-ui-router.min'
],
},
// define js scripts dependencies
shim: {
'bootstrap': {
deps: ['jquery']
},
'angular': {
deps: ['bootstrap'],
exports: 'angular'
},
'angularResource': {
deps: ['angular']
},
'angularAnimate': {
deps: ['angular']
},
'ngMessages': {
deps: ['angular']
},
'uiBootstrap': {
deps: ['bootstrap', 'angular']
},
'uiRouter': {
deps: ['angular']
},
},
priority: [
'angular'
],
deps: ['./ng.app']
});
and in module.js I am requiring ngMessages:
define(function(require) {
'use strict';
var angular = require('angular');
require('angularResource');
require('ngMessages');
require('uiRouter');
require('uiBootstrap');
// angular module definition
return angular.module(
// module name
'companies',
// module dependencies
[
'ngResource',
'ngMessages',
'ui.router',
'ui.bootstrap',
require('shared/fend/input-utils/package').name,
require('shared/fend/pagination/package').name
]
);
});
and then in my controller I am trying to inject ngMessages:
define(function(require) {
'use strict';
var module = require('../module');
require('../resources/rest');
module.controller('CompaniesNewCtrl', CompaniesNewCtrl);
CompaniesNewCtrl.$inject = [
'$rootScope', '$scope', '$state',
'CompaniesResource',
'InputFocusFactory', 'ngMessages'
];
function CompaniesNewCtrl($rootScope, $scope, $state, resource, input, ngMessages) {... })
but i am getting error:
Error: $injector:unpr Unknown Provider Unknown provider:
ngMessagesProvider
What am I doing wrong?
Check API of ngMessages It is directive, not an provider so you can not inject it as dependency in component of angular. You can use it on html as AE (attribute/element)
API
.directive('ngMessages', ['$animate', function($animate) {
var ACTIVE_CLASS = 'ng-active';
var INACTIVE_CLASS = 'ng-inactive';
return {
require: 'ngMessages',
restrict: 'AE',
controller: ['$element', '$scope', '$attrs', function($element, $scope, $attrs) {
//.......code here

ExtJS4: Accessing global variables from Ext.Application

I want to load some application specific settings and save them as global variables when the application is loaded. I have found how to create and access global variable here.
This is how my app.js looks like:
Ext.application({
stores: [
...
],
views: [
...
],
autoCreateViewport: true,
name: 'MyApp',
controllers: [
'DefaultController'
],
launch: function() {
...
}
});
Is it possible to set these variables inside launch: function() block? If not, are there any alternatives?
You can also create a singleton:
Ext.define('MyApp.util.Utilities', {
singleton: true,
myGlobal: 1
});
in your app:
Ext.application({
stores: [
...
],
views: [
...
],
autoCreateViewport: true,
name: 'MyApp',
controllers: [
'DefaultController'
],
requires: ['MyApp.util.Utilities'], //Don't forget to require your class
launch: function() {
MyApp.util.Utilities.myGlobal = 2; //variables in singleton are auto static.
}
});

Resources