Angularjs error - Uncaught Error: [$injector:modulerr] - angularjs

I am a AngularJS beginner and I am using Angular 1.3.15 and I am facing the below error when I try to execute a simple script
Uncaught Error: [$injector:modulerr]
Html
<title>AngularJS data binding</title>
<script src="node_modules/angular/angular.min.js"></script>
<script src="myscript.js"></script>
<div data-ng-controller="SimpleController">
Name :
<br/>
<input type="text" ng-model="name"/>{{name |uppercase}}
<div>
<ul>
<li ng-repeat="personName in names">{{personName}}</li>
</ul>
</div>
</div>
JS file -
(function(){
var app = angular.module('myApp',[]);
app.controller('SimpleController', function($scope) {
$scope.names = ['test1','test2'];
});
})();
Does the code in the file myscript.js has to be in the (function()}) ?
Thanks,

If you are minifying the js files then
app.controller('SimpleController', function($scope) {
$scope.names = ['test1','test2'];
});
this becomes something like
x.controller('SimpleController', function(a) {
a.a = ['test1','test2'];
});
then there is no $scope in the controller so use like,
app.controller('SimpleController', ['$scope', function($scope) {
$scope.names = ['test1','test2'];
}]);
then angular will solve the arguments you pass to functions,
for example:
app.controller('SimpleController', ['$scope', function(a) {
a.names = ['test1','test2'];
}]);
angular will match the a to the $scope property.

Error is coming due to minification. Try this
JS:
(function(){
var app = angular.module('myApp',[]);
app.controller('SimpleController',['$scope', function($scope) {
$scope.names = ['test1','test2'];
}]);
})();
The way you try is minification proof, read here

Related

Failed to instantiate module when minifying javascript files in AngularJS app with ASP.NET MVC

When I try to minify this controller:
Controller2.$inject = ['$scope', 'TestService'];
angular.module('app.controllers')
.controller('TestController', Controller2);
function Controller2($scope, TestService) {
$scope.teste = 'right!';
}
I have this module file:
(function () {
angular.module('app', ['app.controllers', 'app.services', 'socialbase.sweetAlert', 'ngAnimate', 'ui.bootstrap']);
angular.module('app.controllers', []);
angular.module('app.services', []);
})();
and my HTML:
<div ng-app="app">
<div ng-controller="TestController" class="container-fluid" style="padding-top: 25px;">
{{teste}}
</div>
</div>
But when I minify it, it gives me this error:
Failed to instantiate module app due to:
Error: [$injector:nomod] http://errors.angularjs.org/1.6.4/$injector/nomod?p0=app
If I don't minify the javascript files, it works normally.
You need to inject it on the instance. Try it this way
var Controller2 = function($scope, TestService) {
$scope.teste = 'right!';
}
Controller2.$inject = ['$scope', 'TestService'];
angular.module('app.controllers')
.controller('TestController', Controller2);

angular js alert by click donot work

i am new to AngularJS so please forgive me this dump question.
i have error
Cannot set property 'test' of undefined
angularjs
var App = angular.module('StartModule', []);
App.controller('ModalDemoCtrl', [
function($scope) {
$scope.test = function() {
alert("12312");
}
}
]);
html
<body ng-app="StartModule">
<div ng-controller="ModalDemoCtrl">
<div ng-click="test()">11111</div>
</div>
<body>
You just miss $scope to controller's second argument:
var App = angular.module('StartModule', []);
App.controller('ModalDemoCtrl', [ $scope, function($scope) {
$scope.test = function() {
alert("12312");
}
}]);
You're missing the $scope dependency, you need to inject it in your controller :
App.controller('ModalDemoCtrl', ['$scope'
function($scope) {
That's why the $scope variable is undefined, you're not actually injecting any dependency in it.
This uses the Inline Array Annotation: https://docs.angularjs.org/guide/di

Angular.module is not working

I am new to Angularjs and currently practising it .I have a simple example like this .
My View Index.cshtml
<div ng-app="MyApp">
<div class="show-scope-demo">
<div ng-controller="MainController">
<p>Good {{timeOfDay}}, {{name}}!</p>
<div ng-controller="subController1">
<p>Good {{timeOfDay}}, {{name}}!</p>
<div ng-controller="subController2">
<p>Good {{timeOfDay}}, {{name}}!</p>
</div>
</div>
</div>
</div>
</div>
and my controller is MyJsScript.js
(function (angular) {
angular.module('MyApp', []).controller('MainController', ['$scope', function ($scope) {
$scope.timeOfDay = 'morning';
$scope.name = 'sapan';
}]);
angular.module('MyApp', []).controller('subController1', ['$scope', function ($scope) {
$scope.name = 'sapan';
}]);
angular.module('MyApp', []).controller('subController2', ['$scope', function ($scope) {
$scope.timeOfDay = 'Evening';
$scope.name = 'Tapan';
}]);
})(window.angular);
in this case I am getting error
"[ng:areq] Argument 'MainController' is not a function, got undefined"
But if I am changing my controller like this
(function (angular) {
var myApp = angular.module('MyApp', []);
myApp.controller('MainController', ['$scope', function ($scope) {
$scope.timeOfDay = 'morning';
$scope.name = 'sapan';
}]);
myApp.controller('subController1', ['$scope', function ($scope) {
$scope.name = 'sapan';
}]);
myApp.controller('subController2', ['$scope', function ($scope) {
$scope.timeOfDay = 'Evening';
$scope.name = 'Tapan';
}]);
})(window.angular);
It is working perfectly without any error .
Can anyone please tell me what is the exact difference between these two syntax.
Every time you call
angular.module('MyApp', [])
you're defining a new module named "MyApp" (and thus effectively overwrite the module that was previously defined with the same name).
To get a reference to a previously defined module named "MyApp", the correct syntax is
angular.module('MyApp')
without the array of dependencies.

ng-src in iframe is not working

I am using
<div ng-controller="DailyReportsController">
<iframe ng-src="{{content}}" style="width:100%;height:100%;"></iframe>
</div>
to include 'pdf' file in html page. The corresponding controller is given below
mainApp.controller('DailyReportsController',
['$scope', '$state', '$rootScope', 'mainWebService', '$http', '$sce',
function($scope, $state, $rootScope, mainWebService, $http, $sce) {
angular.element(document).ready( function() {
var drtab = new Royal_Tab($('.dailyrpts_tabs.royal_tab'));
$scope.content =$sce.trustAsResourceUrl("https://gcc.geojit.net/researchdesk/OPTION STRATEGIES2.pdf");
}
]);
But following error raises
TypeError: Cannot read property 'nodeName' of undefined
at La (http://localhost:8080/flipxl/scripts/angular.min.js:142:109)
at Object.Kb.$set (http://localhost:8080/flipxl/scripts/angular.min.js:65:380)
at Object.fn (http://localhost:8080/flipxl/scripts/angular.min.js:63:358)
at k.$digest (http://localhost:8080/flipxl/scripts/angular.min.js:109:172)
at k.$apply (http://localhost:8080/flipxl/scripts/angular.min.js:112:173)
at h (http://localhost:8080/flipxl/scripts/angular.min.js:72:454)
at w (http://localhost:8080/flipxl/scripts/angular.min.js:77:347)
at XMLHttpRequest.z.onreadystatechange (http://localhost:8080/flipxl/scripts/angular.min.js:78:420)
Have any solution please share with me
Works perfectly fine for me when adding a $scope.$apply because of the ready event and fixing the missing braces (DEMO, iframes unfortunately don't seem to work in SO snippets).
I was not able to reproduce your error though.
mainApp.controller('DailyReportsController',
['$scope', '$state', '$rootScope', 'mainWebService', '$http', '$sce', function($scope, $rootScope, $http, $sce) {
angular.element(document).ready( function() {
//var drtab = new Royal_Tab($('.dailyrpts_tabs.royal_tab'));
$scope.$apply(function() {
$scope.content = $sce.trustAsResourceUrl("https://gcc.geojit.net/researchdesk/OPTION STRATEGIES2.pdf");
});
});
}]);
You can use directive feature for this as below.
var ngApp = angular.module("ngApp",[]);
ngApp.directive("pdfLoader",function(){
return function(scope,elem,attr){
elem.append("<object width='100%' height='100%' src='"+ scope.content +"' type='application/pdf'></object>");
};
});
ngApp.controller("ngCtrl",function($scope){
$scope.content ="https://gcc.geojit.net/researchdesk/OPTION STRATEGIES2.pdf";
});
and html like below
<div style="height:100%" pdf-loader></div>
Take a look at running example for this at http://jsbin.com/quzoyiloke , And code at http://jsbin.com/quzoyiloke/3/edit?html,js
You can also try by creating new html file whose content is as following.
<html ng-app="myApp">
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.8/angular.js"></script>
<script type="text/javascript">
var myApp = angular.module('myApp',[]);
myApp.directive("pdfLoader",function(){
return function(scope,elem,attr){
elem.append("<object width='100%' height='100%' src='"+ scope.content +"' type='application/pdf'></object>");
};
});
myApp.controller("ngCtrl",function($scope){
$scope.content ="https://gcc.geojit.net/researchdesk/OPTION STRATEGIES2.pdf";
});
</script>
</head>
<body ng-app="ngCtrl">
<div ng-controller="ngCtrl">
<div style="height:100%" pdf-loader></div>
</div>
</body>
</html>

AngularJS - Undefined controller

I have just started an Angularjs tutorial and I fail in running the exercise from the first lesson. It is about creating 2 modules: the first is the bootstraper of the application and the second one contains 2 controllers.
This is the code:
app.js
'use strict';
angular.module('myApp', ['myApp.controllers'])
.run(function ($rootScope) {
$rootScope.title = 'Famouse books';
$rootScope.name = 'Root Scope';
});
controllers.js
angular.module('myApp.controllers', []).controller('SiteController', function ($scope) {
$scope.publisher = 'Site point';
$scope.type = 'Web development';
$scope.name = 'Scope for SiteController';
});
angular.module('myApp.controllers', []).controller('BookController', function ($scope) {
$scope.books = ['Jump Start HTML 5', 'Jump Start CSS', 'Jump Start Responsive Web Design'];
$scope.name = 'Scope for BookController';
});
index.html
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<title ng-bind="title"></title>
</head>
<body ng-controller="SiteController">
<span>{{publisher}} excels in {{type}} books</span>
<div ng-controller="BookController">
<h3>Some of the popular books from {{publisher}}</h3>
<ul>
<li ng-repeat="book in books">{{book}}</li>
</ul>
</div>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.5/angular.min.js"></script>
<script src="scripts/app/controllers.js"></script>
<script src="scripts/app/app.js"></script>
</body>
</html>
I get the following error:
Bad Argument: Argument 'SiteController' is not a function, got undefined
You added extra ,[] in second sentence. This creates a new module and therefore overwrites the first module that has "SiteController" defined - you want to add to the existing module:
angular.module('myApp.controllers', []).controller('SiteController', function ($scope) {
$scope.publisher = 'Site point';
$scope.type = 'Web development';
$scope.name = 'Scope for SiteController';
});
// you added extra ,[]
angular.module('myApp.controllers').controller('BookController', function ($scope) {
$scope.books = ['Jump Start HTML 5', 'Jump Start CSS', 'Jump Start Responsive Web Design'];
$scope.name = 'Scope for BookController';
});
Calling angular.module('myApp.controllers', []) means: Create Module.
Calling angular.module('myApp.controllers') means: Get Module.

Resources