angular argument is not a function, got undefined - javascript

In my html page I get the below error
Error: [ng:areq] Argument 'quickReportController' is not a function, got undefined
I searched and found a lot of solutions but not none of them was helping
The html file
<html>
<head>
...
<script src="~/Scripts/jquery-2.1.3.js"></script>
...
<script src="~/Scripts/angular.js"></script>
<script>
</script>
</head>
<body style="font-family: tahoma;" ng-app>
<div class="container bootcards-container" ng-module="myModule">
<div class="row" ng-controller="quickReportController">
<h3>{{name}}</h3>
</div>
</div>
<script type="text/javascript">
(function () {
var module = angular.module('myModule', []);
module.controller("quickReportController", ['$scope', function ($scope) {
$scope.name = "dd";
}]);
}());
</script>
</body>
</html>

you have missed to define ng-app, it should be ng-app='myModule',
only using
<body style="font-family: tahoma;" ng-app>
will try to find global controller named quickReportController, ehich is not global.
Replace with this:
<body style="font-family: tahoma;" ng-app='myModule'>
Also You are trying to define ng-module and nd ng-app, so read this https://stackoverflow.com/a/22865917/3556874

Have you tried to use ng-app="myModule" and remove ng-module from your html?

Related

JavaScript error: 'angular' is undefined - AngularJS

Should be a basic solution but for the life of me I don't see what is wrong.
I am attempting to run an Angular JS application and I am getting a runtime error:
JavaScript runtime error: 'angular' is undefined
Here is my code below.
(function() {
var app = angular.module('MyApp', []);
app.controller('MyAppController', function($scope) {
$scope.message = "Hello, World!";
});
}());
<!doctype html>
<html ng-app>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
<script src="js/main.js"></script>
</head>
<body>
<h1> Angular JS App </h1>
<div ng-app="MyApp" ng-controller="MyAppController">
<label> {{message}} </label>
</div>
</body>
</html>
Thank you in advance!
You placed your ngApp directive twice in your view template.
https://plnkr.co/edit/cSTHyVqMHFVw8ht9mkIr
<body ng-app="app">
<h1> Angular JS App </h1>
<div ng-controller="ctrl">
<label> {{message}} </label>
</div>
</body>
Declaring once will solve your issue.

angular.min.js:118 Error: [ng:areq]

I'm starting learning Angular.js with this book "Angular.js OReilly", I am trying to construct the first examples that they have. I already downloaded Angular.js from the website and create my controller.js like it says, but I always get the error in the title.
This is what I did:
<html ng-app>
<head>
<script src="angular.min.js"></script>
<script src="controllers.js"></script>
</head>
<body>
<div ng-controller='HelloController'>
<p>{{greeting.text}}, World</p>
</div>
</body>
</html>
function HelloController($scope) {
console.log("a");
$scope.greeting = { text: 'Hello' };
}
You need to put HelloController between tag inside tag.
<head>
<script src="angular.min.js"></script>
<script src="controllers.js"></script>
<script>
function HelloController($scope) {
console.log("a");
$scope.greeting = { text: 'Hello' };
}
</script>
</head>
Why are you using so old syntax? Try to start with new syntax you can do your above requirement like below code:
<html ng-app="myApp">
<head>
<script src="angular.min.js"></script>
<script>
var myApp = angular.module('myApp',[]);//creating app
myApp.controller('GreetingController', ['$scope', function($scope) { // creating controller
$scope.greeting = 'Hola!';
}]);
</script>
</head>
<body>
<div ng-controller="GreetingController">
{{ greeting }}
</div>
</body>
First you have to make app then make a controller. try this

Cannot access http json service in angular js

I am new to angularjs and I want to know why it isn't working.
The code has a flaw and am not sure about the same.
Thinking from java perspective,httpController defined has nested function defined inside.
Here it is my code
index.html
<!DOCTYPE html>
<html >
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>
<body>
<div ng-app="myapp" ng-controller="HelloController">
<h2>{{message}}</h2>
</div>
<div ng-app="httpService" ng-controller="httpController">
<div>FirstName:{{user.name}}</div>
</div>
</body>
</html>
Script.js
var app = angular.module("myapp", []);
app.controller("HelloController", function($scope) {
$scope.message = "Hello, AngularJS";
});
var httpApp=angular.module("httpService",[]);
httpApp.controller("httpController",function($scope,$http){
var onUserComplete=function(response){
$scope.user=""response.data"";
}
$http.get("https://api.github.com/users/rob").then(onUserComplete);
}
);
Only one ng-app will be automatically bootstrapped on your page. That's why if you remove the first ngApp directive, the second one will work.
var httpApp = angular.module("httpService", []);
httpApp.controller("httpController", function($scope, $http) {
var onUserComplete = function(response) {
$scope.user = response.data;
}
$http.get("https://api.github.com/users/rob").then(onUserComplete);
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
<div ng-app="httpService" ng-controller="httpController">
<div>FirstName: {{user.name}}</div>
</div>
NOTE: You have a typo in your callback, remove ""s around your response.data. Also, since you are using Angular 1.5.6, you don't need to specify dependencies/inject your $http service to make your code work.
https://plnkr.co/edit/LyWCLeBeyHPzjys3LOcr?p=preview
<body ng-app="httpService">
<div ng-controller="httpController">
<div>FirstName: {{user.key}}</div>
</div>
</body>
This link is working fine...just try it out
Do not use ng-app more than once in your program...Your code would not work

Load angularJs controller

I had already declared my app and controller in separate file and below is how i am loading my controller html DOM but it is not showing that message. Can someone please guide how to achieve this.
HTML:
<div id="bnProblemList">
<div ng-controller="problemListCtrl" data-ng-init="init()">
<div ng-view="">this is my message = {{message}}</div>
</div>
</div>
JS:
html = $j('#bnProblemList').html();
$compile(html)(scope);
Please let me know how to inject or load ng-controller html dynamically.
Your controller declaration is wrong. You should do it like this:
var myApp = angular.module('myApp',[]);
myApp.controller('problemListCtrl', ['$scope',
function($scope) {
$scope.message = "This is my message :-)";
}
]);
A proper way is to declare your Controller into an anonymous function, and add it to a module. Then, you have to set your ngApp.
Controller.js
Here, we will declare our controller, and create an app module. Then, we will declare ctrl as our controller into our app module.
(function(){
function Controller($scope) {
$scope.name = 'toto';
}
angular
.module('app', [])
.controller('ctrl', Controller);
})();
HTML
<body ng-app='app' ng-controller="ctrl">
<p>My name is {{name}}</p>
</body>
And don't forget to include your script tag :
<script src="controller.js"></script>
You can try Like below Example:-
Html Page:
<!DOCTYPE html>
<html ng-app="MyApp">
<head>
<title> Controller</title>
<script src="../Js/angular.min.js"></script>
<script src="../Js/Controller.js"></script>
</head>
<body>
<div ng-controller="MyFirstController">
{{ Msg }}
</div>
</body>
</html>
ControllerJs Page:
var MyApp=angular.module("MyApp",[]);
MyApp.controller("MyFirstController",function($scope){
$scope.Msg="Hello First Conroller";
})
i was able to load/ inject controller dynamically by the help of below code
<div id="bn" ng-controller="myCtrl as vm">
<div ui-view=''></div>
</div>
<script>
angular.element(document).injector().invoke(function($compile, $state) {
$state.go('myCtrl');
var scope = angular.element($j("#bn")).scope();
$compile($j("#bn"))(scope);
});
</script>

Unable to access form of ng-include file $scope

When I inspect $scope I see firstForm but I don't see secondForm in the object. I am at a loss as to why this is happening.
Here's the sandbox code:
index.html
<html ng-app>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js"></script>
<script>
var firstController = function ($scope){ };
</script>
</head>
<body>
<div ng-controller="firstController">
<form name="firstForm"></form>
<div ng-include="'second_form.html'"></div>
</div>
</body>
</html>
second_form.html
<form name="secondForm"></form>
Thanks to #charlietfl for this suggestion:
Might consider a simple directive to switch with ng-include that just has templatetUrl then won't have that child scope
index.html
<html ng-app="myModule">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js"></script>
<script>
var myModule = angular.module('myModule', []);
myModule.directive('secondForm', function () {
return{
templateUrl: 'secondform.html'
}
});
var firstController = function ($scope){
console.log($scope);
};
</script>
</head>
<body>
<div ng-controller="firstController">
<form name="firstForm">
</form>
<div second-form></div>
</div>
</body>
This works!
ng-include will create another $scope. If you want firstController() to handle both forms the do the following:
<div>
<form ng-controller="firstController" name="firstForm"></form>
<div ng-include="'second_form.html'"></div>
</div>
Then in second_form.html do:
<form ng-controller="firstController" name="secondForm"></form>
use other controllers in for ng-include directives.
In secondForm.html specify controller name as
<div ng-controller="secondCtrl">
</div>
So wen you use ng-inculde inside main controller all controllers of the subtemplates inside ng-include will inherit from the main controller,i.e it will have parent-child relationsship..
More more info go to this link
ng-include and ngRoute: how to make them work together? (i.e. route to a view wihin a ng-include)

Categories

Resources