in my angular application i want to make redirect with $location.path('/');
but i'm getting this error
Uncaught Error: [$injector:modulerr] Failed to instantiate module myApp due to:
Error: [$injector:modulerr] Failed to instantiate module myApp.login due to:
TypeError: Cannot read property 'html5Mode' of undefined
here is my code
angular.module('myApp.login', ['ngRoute', 'angular-md5'])
.config(['$routeProvider', function($routeProvider, $locationProvider) {
$routeProvider.when('/login', {
templateUrl: 'login/login.html',
controller: 'loginCtrl'
});
$locationProvider.html5Mode({
enabled: true,
requireBase: false
}).hashPrefix('!');
}])
You missed to add dependency in the DI array before using it in factory function of a config block.
Code
.config(['$routeProvider', '$locationProvider', //<-- added dependency before using it in function
function($routeProvider, $locationProvider) {
Related
What is the problem with the following code?
(function() {
angular
.module('myapp')
.config(ConfigureProvider);
ConfigureProvider.$inject = ['$routeProvider'];
function ConfigureProvider($routeProvider){
$routeProvider.
when('/',{
templateUrl:'main.html',
controller:'GalleryController'
}).
when('/1',{
templateUrl:'favourites.html',
controller:'FavouritesController'
})
}
})();
I am getting the following error: Uncaught Error: [$injector:nomod] http://errors.angularjs.org/1.5.3/$injector/nomod?p0=myapp
You have not defined your module
angular.module('myapp',[])
and then
angular
.module('myapp')
.config(ConfigureProvider);
ConfigureProvider.$inject = ['$routeProvider'];
function ConfigureProvider($routeProvider){
$routeProvider.
when('/',{
templateUrl:'main.html',
controller:'GalleryController'
}).
when('/1',{
templateUrl:'favourites.html',
controller:'FavouritesController'
})
}
why when the view is rendered the following error was thrown
Uncaught Error: [$injector:modulerr] Failed to instantiate module
registrationModule due to: Error: [$injector:unpr] Unknown provider:
$routeProvider
http://errors.angularjs.org/1.5.0/$injector/unpr?p0=%24routeProvider
at http://localhost:2044/Scripts/angular.js:68:12
at http://localhost:2044/Scripts/angular.js:4397:19
at getService (http://localhost:2044/Scripts/angular.js:4550:39)
at injectionArgs (http://localhost:2044/Scripts/angular.js:4574:58)
at Object.invoke (http://localhost:2044/Scripts/angular.js:4596:18)
at runInvokeQueue (http://localhost:2044/Scripts/angular.js:4497:35)
at http://localhost:2044/Scripts/angular.js:4506:11
at forEach (http://localhost:2044/Scripts/angular.js:321:20)
at loadModules (http://localhost:2044/Scripts/angular.js:4487:5)
at createInjector (http://localhost:2044/Scripts/angular.js:4409:19)
http://errors.angularjs.org/1.5.0/$injector/modulerr?p0=registrationModule&…jector%20(http%3A%2F%2Flocalhost%3A2044%2FScripts%2Fangular.js%3A4409%3A19)
here is my route configuration
var registrationModule = angular.module("registrationModule", [])
.config(function($routeProvider, $locationProvider) {
$routeProvider.when('/Registration/Courses', { templateUrl: '/templates/courses.html', controller: 'CoursesController' });
$routeProvider.when('/Registration/Instructors', { templateUrl: '/templates/instructors.html', controller: 'InstructorsController' });
$locationProvider.html5Mode(true);
});
i'm using Angular JS in MVC5
You need include 'ngRoute'.
var registrationModule = angular.module("registrationModule", ['ngRoute'])
.config(function($routeProvider, $locationProvider) {
$routeProvider.when('/Registration/Courses', { templateUrl: '/templates/courses.html', controller: 'CoursesController' });
$routeProvider.when('/Registration/Instructors', { templateUrl: '/templates/instructors.html', controller: 'InstructorsController' });
$locationProvider.html5Mode(true);
});
I am using the angular seed project and try to use ui-router for user login based routing.
I get following error in the console:
Uncaught Error: [$injector:modulerr] Failed to instantiate module MyApp due to:
Error: [$injector:modulerr] Failed to instantiate module ui.router due to:
Error: [$injector:modulerr] Failed to instantiate module ui.router.state due to:
Error: [$injector:modulerr] Failed to instantiate module ui.router.router due to:
Error: [$injector:modulerr] Failed to instantiate module ui.router.util due to:
TypeError: forEach is not a function
at new $UrlMatcherFactory
...
My code in app.js is:
'use strict';
// Declare app level module which depends on views, and components
angular
.module('MyApp', [
'ui.router'
])
.config(['$stateProvider', function($stateProvider) {
$stateProvider
.state("root", {
url: "",
template: "<section ui-view></section>",
controller: function($state, user) {
if ($state.is("root")) $state.go(user.loggedIn ? "main" : "login");
}
})
.state("login", {
templateUrl: "login/login.html",
controller: "LoginCtrl"
})
.state("main", {
templateUrl: "main/main.html",
controller: "MainCtrl"
});
}])
.controller("MainCtrl", function($scope, user, $state) {
$scope.user = user;
$scope.logout = function() {
user.loggedIn = false;
$state.go("root");
}
})
.controller("LoginCtrl", function($scope, user, $state) {
$scope.login = function() {
user.loggedIn = true;
$state.go("root");
}
});
I didn't find anything with Google and hope someone can help me here.
Best regards
Probably a solution here ?
See this link : https://github.com/ded/script.js/issues/86
I want to know why my config function is not injected properly. this is my module injection code
angular.module("myApp", ['ngRoute'])
.controller('myCtrl', myCtrl)
.controller('common', common)
.config('configuration', configuration)
.factory('cprPostService', cprPostService);
this is my config declaration I get the following error
[$injector:modulerr] http://errors.angularjs.org/1.2.13/$injector/modulerr?p0=myApp&p1=Error%3A%20%5Bng%3Aareq%5D%20http%3A%2F%2Ferrors.angularjs.org%2F1.2.13%2Fng%2Fareq%3Fp0%3Dfn%26p1%3Dnot%2520a%2520function%252C%2520got%2520string%0A%20%20%20at%20yb%20(https%3A%2F%2Fajax.googleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.2.13%2Fangular.min.js%3A18%3A354)%0A%20%20%20at%20Qa%20(https%3A%2F%2Fajax.googleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.2.13%2Fangular.min.js%3A18%3A447)%0A%20%20%20at%20jc%20(https%3A%2F%2Fajax.googleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.2.13%2Fangular.min.js%3A28%3A333)%0A%20%20%20at%20d%20(https%3A%2F%2Fajax.googleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.2.13%2Fangular.min.js%3A30%3A291)%0A%20%20%20at%20Anonymous%20function%20(https%3A%2F%2Fajax.googleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.2.13%2Fangular.min.js%3A29%3A294)%0A%20%20%20at%20r%20(https%3A%2F%2Fajax.googleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.2.13%2Fangular.min.js%3A7%3A278)%0A%20%20%20at%20e%20(https%3A%2F%2Fajax.googleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.2.13%2Fangular.min.js%3A29%3A115)%0A%20%20%20at%20ac%20(https%3A%2F%2Fajax.googleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.2.13%2Fangular.min.js%3A32%3A230)%0A%20%20%20at%20c%20(https%3A%2F%2Fajax.googleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.2.13%2Fangular.min.js%3A17%3A429)%0A%20%20%20at%20%24b%20(https%3A%2F%2Fajax.googleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.2.13%2Fangular.min.js%3A18%3A133)
function configuration($routeProvider, $locationProvider) {
$routeProvider
.when('/List', {
templateUrl: './pages/templates/initialization.html',
controller:'myCtrl'
})
.otherwise({
redirectTo: '/List'
});
};
You need to specify a $inject array for the config fuction:
configuration.$inject = ['$routeProvider', '$locationProvider'];
Actually the error line is
.config('configuration', configuration)
it has to be just,
.config(configuration)
This page: https://docs.angularjs.org/guide/providers has a table at the bottom which states that both constants and providers are available in config phase.
When I try to use some constant in my config I get this error:
Uncaught Error: [$injector:modulerr] Failed to instantiate module testApp due to:
TypeError: undefined is not a function
The constant is set up as follows:
'use strict';
angular.module('services.config', [])
.constant('configuration', {
key: 'value';
});
then the configuration is:
angular
.module('testApp', ['services.config', 'ngRoute'])
.config(function ($routeProvider, configuration) {
$routeProvider.when('/', {
templateUrl: 'views/main.html',
controller: 'MainCtrl'
});
// do something with configuration.key - causes error
});
Does anyone know what I'm doing wrong?
Thanks in advance.
var app = angular.module('TestApp', []);
app.constant('configuration', {
key: 'value'
});
app.config(function (configuration) {
console.log(configuration.key); //value
});
app.constant('configuration', {
key: 'value'
});
here is the jsFiddle: http://jsfiddle.net/gopinathshiva/0701k7ke/8/