I am using grunt-ng-constant to load environment variables into my angular application. This seems to be working 95% correctly with the exception of loading the application. For instance, when i load the app i cannot gain access to the variables, so if i log the ENV base object it comes up as undefined. If i navigate to the next page i am able to log the ENV and gain access to my vars. It seems to me that the app is loading prior to loading the config.js and i am unsure how to change this.
index.html
<script src="scripts/app.js"></script>
<script src="scripts/config.js"></script>
....
app.js
angular
.module('serverSideConfiguratorApp', [
'ngAnimate',
'ngCookies',
'ngResource',
'ngRoute',
'ngSanitize',
'ngTouch',
'angularSpinner',
'smart-table',
'config'
])
gruntfile
ngconstant: {
qa: {
options: {
dest: 'app/scripts/config.js'
},
constants: {
ENV: {
name: 'qa',
server: [{
"name": "QA1 (232)",
"url": "http://xxxxx/"
}, {
"name": "QA2 (233)",
"url": "http://xxxxx/"
}, {
"name": "QA3 (LB)",
"url": "http://xxxxx//"
}]
}
}
}
}
I have tried making config.js first in my index.html but that has no affect. If anyone can provide any insight it would be greatly appreciated! Also, if you need more code or have any questions let me know.
Thanks
As suspected the answer was simple(and annoying). I did not know that injection order into a controller mattered so i had this:
angular.module('serverSideConfiguratorApp')
.controller('ModelsCtrl', [ '$scope', '$http', 'BaseURLService', 'configuratorService', '$q', '$timeout','$route', 'ENV',
function( $scope, $http, BaseURLService, configuratorService, $q, $timeout, ENV)
After changing to this (injecting ENV first):
angular.module('serverSideConfiguratorApp')
.controller('ModelsCtrl', ['ENV', '$scope', '$http', 'BaseURLService', 'configuratorService', '$q', '$timeout','$route',
function(ENV, $scope, $http, BaseURLService, configuratorService, $q, $timeout)
everything now works fine!
Related
I am new to Karma and would really appreciate any help understanding the reason for the error below:
Uncaught Error: [$injector:nomod] Module 'myApp.controllers' is not available!
You either misspelled the module name or forgot to load it. If registering a
module ensure that you specify the dependencies as the second argument.
app.js
angular.module('myApp', [
'ngRoute',
'myApp.controllers',
'myApp.filters',
'myApp.services',
'myApp.directives'
]).
config(function ($routeProvider, $locationProvider) {
controllers.js
angular.module('myApp.controllers', []);
StoreCtrl.js
angular.module('myApp.controllers').
controller('StoreCtrl', ['$scope', '$http', function ($scope, $http) {
StoreCtrl.spec.js
describe('StoreCtrl', function() {
beforeEach(module('myApp.controllers'));
var $controller;
beforeEach(inject(function(_$controller_){
// The injector unwraps the underscores (_) from around the
// parameter names when matching
$controller = _$controller_;
}));
describe('$scope.filterByPrice', function() {
it('test spec', function() {
});
});
});
karma.conf.js
files: [
'public/js/scripts/angular/angular.js',
'public/js/scripts/angular-mocks/angular-mocks.js',
'public/js/scripts/angular-route/angular-route.min.js',
'public/js/app.js',
'public/js/controllers/*.js',
'tests/**/*.spec.js'
],
File Structure
Karma wasn't picking the controllers.js file before the StoreCtrl.js
I had to change the code below:
'public/js/controllers/*.js',
to
'public/js/controllers/controllers.js',
'public/js/controllers/StoreCtrl.js',
and it works now :)
This is for work (I have permission) so I can't post exact code.
So I have to test controllers of a large module. The module has a large config function with a bunch of controllers for the logic of the different pages.
For the actual application it's loaded with bower, which is irritating since I'm testing with Karma-Browserify and npm. So the the dependencies are a mess. I basically have to import everything that was loaded from bower.json to package.json.
This is my karma.conf.js:
module.exports = function(config) {
config.set({
basePath: 'resources',
browserify: {
debug: true,
transform: [ 'browserify-shim' ]
},
browsers: [ 'PhantomJS' ],
captureTimeout: 60000,
client: {
mocha: {}
},
files: [
'tests/assist/test.js',
'assets/scripts/libs/logger.min.js'
],
frameworks: [ 'browserify', 'phantomjs-shim', 'mocha', 'chai' ],
port: 8080,
preprocessors: {
'tests/assist/controller.js': [ 'browserify' ]
},
reporters: [ 'mocha', 'coverage' ],
singleRun: true
});
};
So the code below this is my test.js (removing some company-specific names). Also I need to put angular.mock. or it won't work
require('angular');
require('angular-mocks');
//Main module needs these
jQuery = require('jquery');
require('angular-bootstrap');
require('angular-recaptcha');
require('angular-ui-router');
require('ngstorage');
require(**The path to the main module**);
require(**The path to a service it uses**);
require(**The path to a service it uses**);
require(**The path to a service it uses**);
describe('Blah', function () {
beforeEach(angular.mock.module('myApp'));
var $controller;
beforeEach(angular.mock.inject(function(_$controller_) {
$controller = _$controller_;
}));
describe('blahblah', function () {
it('sets loading to true', function () {
var $scope = {};
var controller = $controller('controller', {$scope: $scope});
assert($scope.showLoading === true);
});
});
});
The main module:
(function() {
'use strict';
})();
// Jquery noconflict
jQuery.noConflict();
var myApp = angular.module('myApp', ['ui.router', 'ngStorage', 'vcRecaptcha', 'ui.bootstrap']);
myApp.config(['$stateProvider', '$urlRouterProvider', '$locationProvider', function($stateProvider, $urlRouterProvider, $locationProvider) {
...
}])
.run([blah bunch of dependencies]) {
...
}]);
The controller (separate fie):
'use strict';
myApp.controller('controller', ['$scope', '$http', '$localStorage', 'service1', 'service2', 'service3',
function ($scope, $http, $localStorage, service1, service2, service3) {
..
}
...
As you can see I'm in dependency hell. I got the example test on the angular site to work, the main problem is with the dependencies and myApp not being visible to the controller. "ReferenceError: Can't find variable: myApp" in controllers/services
If anyone has a better way of going about testing I'm all ears.
This is not about dependency hell, not about testing also.
The code seems to rely on myApp global variable, this is strictly opposite to what Angular modules are for.
myApp should be a local variable that is defined dynamically in each function scope
(function () {
var myApp = angular.module('myApp', [...]);
...
})();
(function () {
var myApp = angular.module('myApp');
myApp.controller('controller', ...)
...
})();
Using 5.2.11 of https://github.com/m-e-conroy/angular-dialog-service/tree/v5.2.11
Angular 1.4.8
I'm adding ui.bootstrap and dialogs.main to my app file:
var FuelTeamworkHelperApp = angular.module( "FuelTeamworkHelperApp", [ "ui.bootstrap", "dialogs.main", "ui.router", "ncy-angular-breadcrumb", "ngResource", "angular-loading-bar", "ngAnimate", "angular-growl", "ngPrettyJson", "angularMoment" ] );
However, I get an error:
Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/1.4.8/$injector/modulerr?p0=FuelTeamworkHelperA…alhost%3A2804%2Fassets%2Fjs%2Fvendor%2Fangular%2Fangular.min.js%3A19%3A463)
Scripts are all definitely in the HTML and browser is loading them.
I tried using 'dialogs.controllers' which works, though according to the documentation that's not how you're meant to use it..!
Not sure how to then get it into my controllers either, which name I should be using? Just 'dialogs' doesn't work.
angular
.module( "FuelTeamworkHelperApp" )
.controller( "AdminSettingsController", AdminSettingsController );
//---
AdminSettingsController.$inject = [ "$scope", "$rootScope", "$state", "$stateParams", "$filter", "$resource", "$q", "growl", "dialogs", "FuelTeamworkHelperConfig", "AppSettings", "SowCustomListSections" ];
function AdminSettingsController ( $scope, $rootScope, $state, $stateParams, $filter, $resource, $q, growl, dialogs, FuelTeamworkHelperConfig, AppSettings, SowCustomListSections ) {
...
Answer to this (as commented by Mike) was the need to include the dependancy ngSanitize.
https://code.angularjs.org/1.4.8/angular-sanitize.min.js
docs.angularjs.org/api/ngSanitize
I want to use lazy loading in my Angular project:
This is the relevant app.js code:
var app = angular.module('eva', ['ui.router',
'controllers', 'oc.lazyLoad']);
app.config(['$stateProvider', '$urlRouterProvider', '$httpProvider', '$locationProvider', '$ocLazyLoadProvider',
function($stateProvider, $urlRouterProvider, $httpProvider, $locationProvider, $ocLazyLoadProvider) {
$httpProvider.interceptors.push('AuthInterceptor');
$urlRouterProvider.otherwise("/");
$locationProvider.hashPrefix('!');
$stateProvider.state('challenge', {
url: '/challenges',
templateUrl: 'views/Challenges.html',
controller: 'ChallengeCtrl',
onEnter: ['$state', 'auth', function($state, auth) {
if (!auth.isLoggedIn()) {
$state.go('login');
}
}],
resolve: { // Any property in resolve should return a promise and is executed before the view is loaded
loadMyCtrl: ['$ocLazyLoad', function($ocLazyLoad) {
// you can lazy load files for an existing module
return $ocLazyLoad.load('js/controllers/ChallengeController.js');
}]
}
This is my controller definition code:
angular.module('eva').controller('ChallengeCtrl', ['$scope', 'auth','$translate', 'challengeFactory', 'userFactory', 'userService',
function($scope, auth, $translate, challengeFactory, userFactory, userService) {
I am not loading the challengecontroller.js file in the index.html file.
I include oclazyload just before app.js in the index.html file:
<script type="text/javascript" src="js/lib/oclazyload/dist/ocLazyLoad.js"></script>
<script type="text/javascript" src="js/app.js"></script>
I get this error now when I run the app:
Error: [ng:areq] http://errors.angularjs.org/1.4.7/ng/areq?p0=ChallengeCtrl&p1=not%20a%20function%2C%20got%20undefined
I tried many things for lazy loading, none of them worked. Now I just followed the example on Example
I really am in a pickle here, and I have no clue what to do to get the lazy loading working. I rather not work with requirejs.
angular config is just a function, any dependencies should be already $inject before you call this config. you may include it in <script> tag and add it to app = angular.module['app', ['depend1'])
so you change it and try it again.
app.config(function($stateProvider, $urlRouterProvider, $httpProvider, $locationProvider, $ocLazyLoadProvider) {
btw: open your Browser Dev-Tools, to check whether this file js/controllers/ChallengeController.js is loadead correctly
I've been struggling trying to hook up this dependency (http://flowjs.github.io/ng-flow/) to my mean.js application. I'm thinking it's simply a naming problem.
The error I'm getting is: Error: [$injector:unpr] Unknown provider: flowProvider <- flow
I've tried 'flow', 'Flow', 'ngFlow', etc. Any help would be greatly appreciated.
modules/core/client/app/config.js
'use strict';
// Init the application configuration module for AngularJS application
var ApplicationConfiguration = (function() {
// Init module configuration options
var applicationModuleName = 'mean';
var applicationModuleVendorDependencies = ['ngResource', 'ngAnimate', 'ui.router', 'ui.bootstrap', 'ui.utils', 'angularFileUpload', 'flow'];
// Add a new vertical module
var registerModule = function(moduleName, dependencies) {
// Create angular module
angular.module(moduleName, dependencies || []);
// Add the module to the AngularJS configuration file
angular.module(applicationModuleName).requires.push(moduleName);
};
return {
applicationModuleName: applicationModuleName,
applicationModuleVendorDependencies: applicationModuleVendorDependencies,
registerModule: registerModule
};
})();
modules/properties/client/properties.client.controller.js:
'use strict';
var $ = $ || {};
// Properties controller
angular.module('properties').controller('PropertiesController', [
'$scope',
'$stateParams',
'$location',
'Authentication',
'Brands',
'Applications',
'Properties',
'flow',
function($scope, $stateParams, $location, Authentication, Brands, Applications, Properties, flow) {
.......
modules/properties/client/properties.client.module.js:
'use strict';
// Use applicaion configuration module to register a new module
//ApplicationConfiguration.registerModule('properties');
ApplicationConfiguration.registerModule('properties',['flow']);
config/assets/default.js:
'use strict';
module.exports = {
client: {
lib: {
css: [
'public/lib/bootstrap/dist/css/bootstrap.css',
'public/lib/bootstrap/dist/css/bootstrap-theme.css'
],
js: [
'public/lib/angular/angular.js',
'public/lib/angular-resource/angular-resource.js',
'public/lib/angular-animate/angular-animate.js',
'public/lib/angular-ui-router/release/angular-ui-router.js',
'public/lib/angular-ui-utils/ui-utils.js',
'public/lib/angular-bootstrap/ui-bootstrap-tpls.js',
'public/lib/angular-file-upload/angular-file-upload.js',
'public/lib/jquery/dist/jquery.js',
'public/lib/ng-flow/dist/ng-flow-standalone.js'
],
tests: ['public/lib/angular-mocks/angular-mocks.js']
},
css: [
'modules/*/client/css/*.css'
],
less: [
'modules/*/client/less/*.less'
],
sass: [
'modules/*/client/scss/*.scss'
],
js: [
'modules/core/client/app/config.js',
'modules/core/client/app/init.js',
'modules/*/client/*.js',
'modules/*/client/**/*.js'
],
views: ['modules/*/client/views/**/*.html']
},
server: {
allJS: ['gruntfile.js', 'server.js', 'config/**/*.js', 'modules/*/server/**/*.js'],
models: 'modules/*/server/models/**/*.js',
routes: ['modules/*[!core]/server/routes/**/*.js', 'modules/core/server/routes/**/*.js'],
sockets: 'modules/*/server/sockets/**/*.js',
config: 'modules/*/server/config/*.js',
policies: 'modules/*/server/policies/*.js',
views: 'modules/*/server/views/*.html'
}
};
So long as you loaded the js file correctly, you can use flowFactory to create a flow instance. Then create a flow object and use that to refer to flow.
angular.module('properties').controller('PropertiesController', [
'$scope',
'$stateParams',
'$location',
'Authentication',
'Brands',
'Applications',
'Properties',
'flowFactory',
function($scope, $stateParams, $location, Authentication, Brands, Applications, Properties, flowFactory) {
$scope.existingFlowObject = flowFactory.create({
target: 'http://example.com/upload'
});
................................................................
So try changing flow to flowFactory and see if that leads to any provider dependency issues.
I don't see an answer to this. Changing the registerModule part.
ApplicationConfiguration.registerModule('flow');
to .client.module.js
This is mean.js version 0.4.0
you don't have to include it into your project dependency
I modified it in this way :
var applicationModuleVendorDependencies = ['ngResource', 'ngAnimate', 'ui.router', 'ui.bootstrap', 'ui.utils', 'angularFileUpload'];