Why does Angular say "Uncaught ReferenceError: $scope is not defined"? - javascript

I am not using scope outside of the controller so I am confused why I keep getting the error:
Uncaught ReferenceError: $scope is not defined
It is from line 5 which is the "pollApp.controller" line.
(app.js)
var pollApp = angular.module('myApp', ['ui.router']);
pollApp.controller('choiceCtrl', [$scope, function ($scope) {
$scope.choices = [{body: "test"}]; //just for testing
$scope.addChoice = function () {
//add new choice
if ($scope.choiceBody) {
$scope.choices.push({
body: $scope.choiceBody
});
$scope.choiceBody = null;
}
}
}]);
I also checked that Angular is loading fine. Any help would be awesome. Thanks.

While asking for dependency using in DI array annotation, you need to wrap it a quotes like '$scope' inside array and then you could have instance of those dependency inside factory function on controller like $scope
pollApp.controller('choiceCtrl', ['$scope', function ($scope) {

pollApp.controller('choiceCtrl', ['$scope',
missing quotes

Related

I have an empty function that's deregistering my controllers in Angularjs

Using Angularjs 1.6.3, I have a controller like this:
'use strict';
var ngnControllers = angular.module('ngnControllers');
ngnControllers.controller('TestCtrl', ['$scope', '$location', '$http',
function TestCtrl($scope, $location, $http) {
console.log("Before defining $scope.play()");
$scope.play() = function() {
};
console.log("After defining $scope.play()");
}
]);
I copied the $scope.play() function from another controller where it was having the same effect:
When it is not commented out, I get a message saying that the controller is not registered, but that's all. When it is commented out everything works fine.
I am at a loss. Any ideas?
Because your function declaration syntax is wrong and as a result is throwing an error. The error gets caught internally and simply causes the controller registration to fail
Change:
$scope.play() = function() {
To
$scope.play = function() {
You should have empty dependencies added to your module,
var ngnControllers = angular.module('ngnControllers',[]);

Angular doesn't work when defining a service and factory separately

I'm trying to write a simple angular service and a factory like below:
html:
<div ng-controller="mycontroller">
{{saycheese}}
</div>
Javascript
var myApp = angular.module('myApp', []);
myApp.service('myservice', function() {
this.sayHello = function() {
return "from service";
};
});
myApp.factory('myfactory', function() {
return {
sayHello: function() {
return "from factory!"
}
};
});
//defining a controller over here
myapp.controller("mycontroller", ["myfactory", "myservice", function(myfactory, myservice) {
$scope.saycheese = [
myfactory.sayHello(),
myservice.sayHello()
];
}]);
But the JSFiddle still just displays {{saycheese}} instead of angular mapping the function.
Link to my fiddle:
http://jsfiddle.net/PxdSP/3047/
Can you point me where am I going wrong in this case ? Thanks.
You have several syntax errors in your code, and checking the console would have helped without questioning the SO. Here's one possible way to write the controller (demo):
myApp.controller("mycontroller", ["$scope", "myfactory", "myservice",
function($scope, myfactory, myservice) {
$scope.saycheese = [
myfactory.sayHello(),
myservice.sayHello()
];
}]);
Apart from obvious fix myapp => myApp (variable names are case-sensitive in JavaScript), $scope should be passed into controller as an argument (and mentioned as its dependency if using arrayed - proper - form of controller definition, as you did) before you can access it. Otherwise you just get ReferenceError: $scope is not defined exception when controller code is invoked.
Couple things:
myapp.controller(...) should be myApp.controller(...)
You need to inject $scope in your controller.
Fixed controller:
myApp.controller("mycontroller", ["myfactory", "myservice", "$scope", function(myfactory, myservice, $scope) {
$scope.saycheese = [
myfactory.sayHello(),
myservice.sayHello()
];
}]);

Code keeps returning "Uncaught SyntaxError: Unexpected token" errors

I keep running into Uncaught SyntaxError: Unexpected token errors, but I can't see where I'm missing/have an extra ; or ) character. It gives me a line in the code (see below), but it appears to be a valid token placement.
JSLint gives me some clarification: Expected ')' to match '(' from line 2 and instead saw ';'.
Below is the JavaScript code I'm writing (inner sections removed for brevity):
'use strict';
(function() {
AppCtrl = function($scope) {
//JSON data
};
window.AppCtrl = AppCtrl;
}; //Error shows up on this line
// Declare app level module which depends on views and components
angular.module('careApp', [
//Dependencies
])
.config(['$routeProvider', function($routeProvider) {
//if given invalid partial, go back to appLaunch.html
$routeProvider.otherwise({
redirectTo: '/appLaunch'
});
}])
.controller('AppCtrl', ['$scope', window.AppCtrl]);
var patientID = ''; $scope.idStore = function() {
//stuff
}
$scope.patientTime = function() {
//more stuff
}
})();
Brackets are wrong, see picture. I had some time off.
Many thanks to #MikeC for his help in solving the problem. While the indentation was a critical part of the issue, it didn't solve the underlying problem: how the variable AppCtrl was defined.
To solve it, let's look at the various ways we could define AppCtrl:
app.controller('MyCtrl', ['$scope', function ($scope) {...}])
app.controller('MyCtrl', function ($scope) {...})
var MyCtrl = function ($scope) {...})
We are currently using the third definition, which isn't working in our favor now. The first definition appears to be closer to what we want. With that in mind...
'use strict';
// Declare app level module which depends on views and components
angular.module('careApp', [
//Dependencies
])
.config(['$routeProvider', function($routeProvider) {
//if given invalid partial, go back to appLaunch.html
$routeProvider.otherwise({redirectTo: '/appLaunch'});
}])
.controller('AppCtrl', ['$scope', function($scope){
$scope.list = [
//JSON data
];
var patientID = '';
$scope.idStore = function() {
//Stuff
}
$scope.patientTime = function(){
//More stuff
}
}]);
...Here's the correct configuration.
Note that we removed the (function() { block up at the very top of the JS file, as well as the appropriate closing elements at the very bottom. We also moved the $scope.list and other $scope functions into the .controller portion of the file.

Global variable in angularjs

i'm looking for a way to use a global varable in a angularjs function.
I found this way, to use the global var as a angular var:
var AppSettings = angular.module('settingsApp', []);
AppSettings.controller('settingsCtrl', ['$scope', '$window', function($scope, $window) {
$scope.sessionUID = $window.sessionUID;
/** function to update user details after edit from list of user php **/
$scope.changefunction = function() {
alert("perfect");
};
}]);
var MyApp = angular.module('biqs_settings_app', ['settingsApp']);
But now I cant run the "changefunction".
Error:
TypeError: Cannot read property '0' of null
at $parseFunctionCall (angular.js:12346)
at callback (angular.js:21435)
at Scope.$eval (angular.js:14401)
at Scope.$apply (angular.js:14500)
at HTMLButtonElement.<anonymous> (angular.js:21440)
at HTMLButtonElement.eventHandler (angular.js:3014)
What am i doing wrong?
Everything will work, if you properly use the settingsCtrl controller in your HTML:
<div ng-app="biqs_settings_app" ng-controller="settingsCtrl">
<button ng-click="changefunction()">Test</button>
</div>

Error: [$injector:nomod] Module is not available

I am trying to upload a file with progress bar but getting 2 errors constantly, no matter what i do
1)module not defined and 2nd is [$injector:nomod] module not available.
http://jsfiddle.net/3m75wqt1/
this is my controller
PageController.js
var PageController = function ($scope, fileUpload ) {
angular.module('app', ['ngProgress'])
angular.module('app')
.controller('PageController', function ($scope) {
//$scope.fileUpload = {};
});
PageController.$inject = ['$scope','ngProgress'];
};
I have done a lot of searching but unable to find the error.
any help would be appreciated... Thanks in advance
First at all the module method shoud not be called inside of a controller, so this:
var PageController = function ($scope, fileUpload) {
angular.module('app', ['ngProgress'])
angular.module('app')
.controller('PageController', function ($scope) {//I will replace this with PageController
//$scope.fileUpload = {};
});
PageController.$inject = ['$scope', 'ngProgress'];
};
shoud look more like this:
var PageController = function ($scope, fileUpload) {
};
PageController.$inject = ['$scope', 'ngProgress'];
angular.module('app', ['ngProgress'])
angular.module('app').controller('PageController', PageController);
i removed the function in your controller method and put your defined PageController at its place
The second thing is that you dont need to call the module method twice, the first one is already returning the module, so the best approach shoud look like this:
var PageController = function ($scope, fileUpload) {
};
PageController.$inject = ['$scope', 'ngProgress'];
angular.module('app', ['ngProgress']).controller('PageController', PageController);
And i personaly like the array notation more than this, so my favorite would look like this, but that is up to you:
angular.module('app', [
'ngProgress'//this is a module dependency (its also createt with angular.module)
]).controller('PageController', [
'$scope',
'fileUpload',//this is a dependency for the controller (probably a service)
function ($scope, fileUpload) {
}
]);

Categories