0

I am getting this error Argument 'MyController' is not a function, got undefined when I am trying to run code below.

<!DOCTYPE html>
<html lang="en" ng-app>

<head>
  <meta charset="utf-8" />
  <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
  <title>Hello world from Prateek</title>
</head>

<body>
  <div ng-controller="MyController">
    <h1>{{author.name}}</h1>
    <p>{{author.title + ', ' + author.company}}</p>
  </div>
  <script>
    function MyController($scope) {
      $scope.author = {
        'name': 'J Doe',
        'title': 'Designer',
        'company': 'ABC XYZ'
      }
    }
  </script>
</body>

</html>

I want to get the name, title and company to be displayed, instead I am getting output as below

{{author.name}} {{author.title + ', ' + author.company}}

3
  • The global function controller syntax isn't available in newer versions of Angular (1.3+). Commented Aug 23, 2015 at 16:01
  • Any suggestions for a workaround? Commented Aug 23, 2015 at 16:03
  • 2
    You don't need a workaround. Just use Angular the proper way. docs.angularjs.org/guide/controller Commented Aug 23, 2015 at 16:04

2 Answers 2

2

You haven't defined any angular application in your javascript. You can define your controller using created module. Then you can access your controller from your views. Here is an example:

<!DOCTYPE html>
<html lang="en" ng-app ="myApp">

<head>
  <meta charset="utf-8" />
  <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
  <title>Hello world from Prateek</title>
</head>

<body>
  <div ng-controller="MyController">
    <h1>{{author.name}}</h1>
    <p>{{author.title + ', ' + author.company}}</p>
  </div>
  <script>
    var myApp = angular.module("myApp", []); // definition of a module, you use myApp in ng-app directive in the html part so that angularJS initialize itself.
    myApp.controller("MyController", function ($scope) {
      $scope.author = {
        'name': 'J Doe',
        'title': 'Designer',
        'company': 'ABC XYZ'
      }
    });
  </script>
</body>

</html>

Sign up to request clarification or add additional context in comments.

4 Comments

That works great, but if I put the javascript into separate controllers.js, I am not able to see the output once again.
Did you include your new javascript in your html using <script> tag
Nopes, no tags. It looks like this could be a positioning problem, where to put the script tag when using controllers.js
You can put it at the end of <body> tag. If your controllers.js is at the same level with your index.html following will work <script src="controllers.js"></script>
2

you should create module first then create controller like:

var app = angular.module('myApp', []);
app.controller('MyController', function($scope) {
    $scope.author = {
        'name': 'J Doe',
        'title': 'Designer',
        'company': 'ABC XYZ'
      }
});

and in your html you should use ng-app="module-name" like:

<html lang="en" ng-app="myApp">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.