Error: [ng:areq] Argument 'PreviewController' is not a function, got undefined

909 views
Skip to first unread message

Vedant Nighojkar

unread,
Oct 9, 2015, 10:12:22 AM10/9/15
to AngularJS
I am getting the following error when load my page.
Error: [ng:areq] Argument 'PreviewController' is not a function, got undefined


I am not sure why I am getting the error. I tried to research on it but I was not able to find any solution.

Sander Elias

unread,
Oct 9, 2015, 10:30:13 AM10/9/15
to AngularJS
Hi Vendant,

Try this in your plunker:
<!DOCTYPE html>
<html>
<head>
   
<link rel="stylesheet" href="style.css">
   
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.js"></script>
   
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular-animate.js"></script>
   
<script>
       
//My module is already created elsewhere
       
(function() {
           
'use strict';
            angular
               
.module('csui', ['ngAnimate'])
               
.controller('PreviewController', preview);

           
function preview($http) {
               
var vm = this;

                $http
.get("https://api.myjson.com/bins/30e2a")
                   
.then(function(response) {
                       
var data = response.data;
                       
//Dummy data taken from JSON file
                        vm
.firstName = data.firstName;
                        vm
.lastName = data.lastName;
                   
});

               
//Functions have been defined. Functionality to be added.
                vm
.addDataSet = function() {
                    alert
("Function not defined");
               
};
           
}
       
}());
   
</script>
</head>
<body ng-app='csui'>
   
<div ng-controller="PreviewController as prevCtrl">
        First Name: {{prevCtrl.firstName}}
<br>
        Last Name: {{prevCtrl.lastName}}
<br>
       
<button ng-click="prevCtrl.addDataSet()">Add</button>
   
</div>
</body>
</html>


Regards
Sander

Vedant Nighojkar

unread,
Oct 9, 2015, 12:42:05 PM10/9/15
to AngularJS
That got rid of the error, and it is loading the JS file! Thank you!

ALEKHYA YANNAVARAPU

unread,
May 24, 2016, 3:46:19 PM5/24/16
to AngularJS
thanks a lot ur code helped .....but why dis code was not working http://jsfiddle.net/wskta72o/... 

Sander Elias

unread,
May 25, 2016, 12:15:52 AM5/25/16
to AngularJS
Hi Alekhya,

  1. syntax errors, putting <script> ... </script> inside JS does not work
  2. defining your controller in the global namespace isn't allowed for a long time already
  3. "controller as vm" is not supported in version 1.0, you need a newer angular for that
I updated your fiddle to work,

Regards
Sander
Reply all
Reply to author
Forward
0 new messages