Controller is not registered error after adding ngRoute `.config` - angularjs

Controller is not recognizing - AngularJS error
I've a controller defined in controllerPloyee.js called controllerPloyee which was working before i added the ngRoute and made to route. The error that shows is
angular.min.js:127 Error: [$controller:ctrlreg] http://errors.angularjs.org/1.7.8/$controller/ctrlreg?p0=controllerPloyee
I've checked the documentation and another questions with the same error, but doesn't help.
Index.html
<html ng-app="ployee">
<head>
<!--Angular-->
<script src="lib/angular.min.js"></script>
<script src="lib/angular-route.js"></script>
<!--JS-->
<script src="assets/js/moduloPloyee.js"></script>
<!--Controllers-->
<script src="assets/js/controllers/controllerPloyee.js"></script>
<!--Services-->
<script src="services/routeConfig.js"></script>
</head>
<body>
<div ng-view></div>
<div ng-include="'view/footer.html'"></div>
</body>
</html>
routeConfig.js
angular.module('ployee', ['ngRoute']).config(function($routeProvider){
$routeProvider.when('/login', {
templateUrl: "view/login.html",
controller: "controllerPloyee"
}).when('/',{
templateUrl: "view/login.html",
controller: "controllerPloyee"
}).otherwise({redirectTo: "/login"})
})
controllerPloyee.js
angular.module('ployee').controller('controllerPloyee', function($scope){
});

The script that creates the module needs to be placed before the others:
<!--Angular-->
<script src="lib/angular.min.js"></script>
<script src="lib/angular-route.js"></script>
<!-- IMPORTANT needs to be place here -->
<script src="services/routeConfig.js"></script>
<!--JS-->
<script src="assets/js/moduloPloyee.js"></script>
<!--Controllers-->
<script src="assets/js/controllers/controllerPloyee.js"></script>
<!--Services-->
̶<̶s̶c̶r̶i̶p̶t̶ ̶s̶r̶c̶=̶"̶s̶e̶r̶v̶i̶c̶e̶s̶/̶r̶o̶u̶t̶e̶C̶o̶n̶f̶i̶g̶.̶j̶s̶"̶>̶<̶/̶s̶c̶r̶i̶p̶t̶>̶
The statement angular.module('ployee', ['ngRoute']) creates a new module. The statement angular.module('ployee') retrieves the module for further configuration.
When the module is created after the controller script, the controller is lost. This is why the script that creates the module needs to be placed before other scripts which add controllers, services, filters, directives, constants, etc.
A module is a container for your app.
Passing one argument retrieves an existing angular.Module, whereas passing more than one argument creates a new angular.Module.
From the Docs:1
Creation versus Retrieval
Beware that using angular.module('myModule', []) will create the module myModule and overwrite any existing module named myModule. Use angular.module('myModule') to retrieve an existing module.
For more information, see
AngularJS angular.module Function API Reference
AngularJS angular.Module Type API Reference
AngularJS Developer Guide - modules

Related

getting Error: [$injector:modulerr] using angularjs datatable?

I am creating one small demo for getting user list and also login using angularjs with web api.and I am using datatable for showing user list.then I am include datatable in module file then first time run the project getting like this error.and then remove 'datatable' from the module working very well.so i can not understand where is my mistke.
this is my app.js code:
var app = angular.module("Demo", ['ngRoute','datatables']);
app.config(['$routeProvider', function ($routeProvider) {
$routeProvider.
when('/dashboard', {
templateUrl: 'home/dashboard',
controller: 'dashboardcontroller'
}).
otherwise({
redirectTo: '/home/login'
});}]);
this is my included datatable module. i am remove here datatable it's working fine but again add datatable getting error from the module.
this is my script ordering :
<script src="~/assets/js/jquery.js"></script>
<script src="~/assets/angular.min.js"></script>
<script src="~/assets/js/angular-datatables.js"></script>
<script src="~/assets/angular-route.min.js"></script>
<script src="~/assets/js/jquery.datatables.js"></script>
<script src="~/assets/js/angular.js"></script>
<script src="~/assets/app.js"></script>
<script src="~/assets/services.js"></script>
<script src="~/assets/controller.js"></script>
this is error is getting:
so this is my code any one know where is mistake then please let me know.
You need angular-datatable plugin also for inject datatable service in controller
see angular-datatable
Please remove the duplicated import of angular and add the jQuery before the angular's files. Like this:
<script src="~/assets/js/jquery.js"></script>
<script src="~/assets/js/jquery.datatables.js"></script>
<script src="~/assets/js/angular.js"></script>
<script src="~/assets/angular-route.min.js"></script>
<script src="~/assets/js/angular-datatables.js"></script>
<script src="~/assets/app.js"></script>
<script src="~/assets/services.js"></script>
<script src="~/assets/controller.js"></script>
The angular-datatables provides a datatables directive you can add to your <table>
Like this <table datatable="">...</table>
You can have more information here: http://l-lin.github.io/angular-datatables/archives/#!/gettingStarted
<script src="~/assets/js/jquery.js"></script>
<script src="~/assets/js/jquery.datatables.js"></script>
<script src="~/assets/js/angular.js"></script>
<script src="~/assets/angular-route.min.js"></script>
<script src="~/assets/js/angular-datatables.js"></script>
<script src="~/assets/app.js"></script>
<script src="~/assets/services.js"></script>
<script src="~/assets/controller.js"></script>
Try with this

Can't inject UI Bootstrap

I have off the shelf app framework. Index.html contains this
<!-- jQuery and Bootstrap -->
<script src="js/jquery/jquery-2.1.1.min.js"></script>
<script src="js/plugins/jquery-ui/jquery-ui.js"></script>
<script src="js/bootstrap/bootstrap.min.js"></script>
<!-- MetsiMenu -->
<script src="js/plugins/metisMenu/jquery.metisMenu.js"></script>
<!-- SlimScroll -->
<script src="js/plugins/slimscroll/jquery.slimscroll.min.js"></script>
<!-- Peace JS -->
<script src="js/plugins/pace/pace.min.js"></script>
<!-- Custom and plugin javascript -->
<script src="js/inspinia.js"></script>
<!-- Main Angular scripts-->
<script src="js/angular/angular.min.js"></script>
<script src="js/plugins/oclazyload/dist/ocLazyLoad.min.js"></script>
<script src="js/ui-router/angular-ui-router.min.js"></script>
<script src="js/bootstrap/ui-bootstrap-tpls-0.12.0.min.js"></script>
<!-- Our application's scripts -->
<script src="js/controllers.js"></script>
and in js/controllers.js, I have
angular
.module('inspinia')
.controller('MainCtrl', MainCtrl)
and my controller is
function MainCtrl($scope, $http, $interval, $state, $location)
when I try to inject UI bootstrap by chaning it to
.module('inspinia', ['ui.bootstrap'])
I get
"Error: [$injector:unpr]
http://errors.angularjs.org/1.3.7/$injector/unpr?p0=%24stateProvider%20%3C-%20%24state%20%3C-%20MainCtrl
I am sure that it is something very simple, but I can't see what.
Seems like you are using $state API service inside your controller so you also need to also include ui.router with it.
So while asking for $state dependency, injector doesn't able to find asked dependency inside angular DI container $injector/unpr?p0=%24stateProvider in MainCtrl
angular
.module('inspinia', ['ui.bootstrap', 'ui.router'])

AngularJS controller reference becomes undefined

I've just started learning AngularJS and built a small yet simple application, which consists of multiple controllers as shown here:
I have a SiteMaster.html page, inside this page I use ng-view as shown here:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="Scripts/angular.min.js"></script>
<script src="Scripts/angular-route.min.js"></script>
<script src="Scripts/app.js"></script> <!-- Consists of Routing -->
<script src="Templates/Params/paramsController.js"></script>
<title>Angular Routing Tutorial</title>
</head>
<body ng-app="mainApp">
<h1>sitemaster Template</h1>
<div ng-view></div>
</body>
</html>
As you can see the last script tag is the controller for the params page, Ideally I don't want this script tag reference on the SiteMaster instead I would like to place it inside the params.HTML page that way it's only loaded when it's required, now I've moved this script tag from the SiteMaster.Html to the params.Html page as shown here:
<script src="paramsController.js"></script>
<div ng-controller="paramsCtrl">
<h1>
Passed a parameter via url
</h1>
</div>
Yet when I run the project I get the following error:
Error: [ng:areq] http://errors.angularjs.org/1.5.2/ng/areq?p0=paramsCtrl&p1=not%20a%20function%2C%20got%20undefined
So my question is, how do I get this paramsController.js reference to work within the params.html page? instead of having it on the SiteMaster.html page?
Please note, when the paramsController.js is placed inside the SiteMaster.Html page it works as expected.
Any help would be appreciated.
Update
After adding ocLazyLoad I have done the following:
Added the script reference to sitemaster.html
Inside my app.js I have done the following:
var app = angular.module('mainApp', ['ngRoute', "oc.lazyLoad"]);
Inside my paramsController.js I have the following:
angular.module("mainApp").controller('paramsCtrl', function ($scope, $routeParams, $ocLazyLoad) {
console.log($routeParams);
var param1 = $routeParams.page_number;
alert(param1);
});
And now I've hit a road block I'm unsure where or how I go about loading this controller via ocLazyLoad ? I following this documentation : https://oclazyload.readme.io/docs
First of all, your path is wrong. Change
<script src="paramsController.js"></script>
to
<script src="Templates/Params/paramsController.js"></script>
Also, include the following in your <head> section:
<base href="/">
And the second thing, I'm not sure, even that will not work as Angular has already been initialized. So you need to use a library like ocLazyLoaded to dynamically load resources.
Also, consider reading this post https://stackoverflow.com/a/17675432/2405040
Update:
In your params.html
<!-- Remove this line
<script src="paramsController.js"></script>
-->
<div ng-controller="paramsCtrl">
<h1>
Passed a parameter via url
</h1>
</div>
And now modify your $routeProvider configuration:
(From the docs https://oclazyload.readme.io/docs/with-your-router)
$routeProvider.
when('/foo', {
templateUrl: 'Templates/Params/params.html',
resolve: {
loadMyCtrl: ['$ocLazyLoad', function($ocLazyLoad) {
// you can lazy load files for an existing module
return $ocLazyLoad.load('Templates/Params/paramsController.js');
}]
}
})

why this angular file does not work?

Why doesn't this code work? I am trying to find it during last four days...
Error: [ng:areq] Argument 'mainCtrl' is not a function, got undefined
HTML:
<!doctype html>
<html ng-app>
<head>
<title> test angular html </title>
<script src="bower_components/angular/angular.js"> </script>
<script>
function mainCtrl($scope) {
$scope.value = 100;
}
</script>
</head>
<body ng-controller="mainCtrl">
<h1> {{value}} </h1>
</body>
</html>
When you are using a framework like angular you must declare some logic to work (angular 1.3+), for example.
You must create the main module of your app:
angular.module('yourmodule', []) // the last parameter [] create the module, that array are the dependencies
With your module created you must attach the controller to the module, you have a function mainCtrl created, i'm gonna use it:
var module = angular.module('yourmodule') // without the second argument, get the module with that name
module.controller('mainCtrl', mainCtrl) // This assign the name mainCtrl the function mainCtrl
And finally, add to ng-app the module created:
<html ng-app="yourmodule">
I hope this work, and welcome to angular world!!!

Routing Function in AngularJS Does Not Get Executed

Newbie with AngularJS here.
I have an index.html file below, which references an Javascript file named app.js. The index.html file is as below:
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<script data-require="angular.js#*" data-semver="1.2.22" src="https://code.angularjs.org/1.2.22/angular.js"></script>
<script data-require="angular-route#*" data-semver="1.2.20" src="https://code.angularjs.org/1.2.20/angular-route.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="app.js"></script>
<script src="controller1.js"></script>
</head>
<body>
<h1>Github Viewers</h1>
<div ng-view></div>
</body>
</html>
In my app.js file, I have the following codes:
(function(){
var app = angular.module("myApp", ["ngRoute"]);
app.config(function($routeProvider){
$routeProvider
.when("/main", {
templateUrl: "main.html",
controller: "controller1"
})
.otherwise({redirectTo: "/main"});
});
}());
I've set breakpoints (in Chrome Dev Tools) on the line:
templateUrl: "main.html",
But I am unable to get into this line.
Console Window shows no errors, so I am a little stumped.
Appreciate any insight on where to look further on this.
You are using the wrong app name.
Change <html ng-app="myApp"> to <html ng-app="githubViewer">
Edit
You cannot put a breakpoint in that line (templateUrl: "main.html"). Note that's an Object definition and not a function:
{
templateUrl: "main.html",
controller: "controller1"
}
And like all Objects, they are defined in an atomic operation. It's like if all its properties were created at once, meaning that you cannot "break" the execution for a specific property.
I've tested with Chrome dev tools and it doesn't even allow me to set a breakpoint in that line. Not sure how you did it...
If you want to check if the "main.html" template is being loaded, watch the network tab of the Chrome dev tools.
I think your dependency injection is missing controller1 module..
if you managing controllers in different module you have to inject it in your app.js
//controller1.js
angular.module('controllers',[])
.controller('controller1',function(....)
//app.js
var app = angular.module("myApp", ["controllers","ngRoute"]);
...
//index.html
...
<script src="controller1.js"></script>
<script src="app.js"></script>
...

Resources