2

I just started with VS2015 ASP.Net5 using Angular. I get an error when using the angular retrieved by Bower:

Error: [ng:areq] Argument 'TextController' is not a function, got undefined http://errors.angularjs.org/1.4.3/ng/areq?p0=TextController&p1=not%20a%20function%2C%20got%20undefined

This is my html code:

<!DOCTYPE html>
<html ng-app>
<head>
    <meta charset="utf-8"/>
</head>
<body ng-controller="TextController">
    <p>{{SomeText}}</p>
    <!--does not work-->
    <script src="lib/angular/angular.js"></script>
    <!--works-->
    <!--<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js">                              </script>-->    
    <script>
        function TextController($scope) {
            $scope.SomeText = "Go";
        }
    </script>
</body>

When I use the Angular Google provides it works like a charm. Below is a what a picture of what was generated using Bower in VS

partial project view

What am I missing?

UPDATE This is the working version thanks to Claies:

<!DOCTYPE html>
<html ng-app="myApp">
<head>
    <meta charset="utf-8"/>
</head>
<body ng-controller="TextController">
    <p>{{SomeText}}</p>
    <!--does not work-->
    <script src="lib/angular/angular.js"></script>
     <script>
        angular.module('myApp', []).controller('TextController', function ($scope) {
            $scope.SomeText = "Go";
        });
    </script>
 </body>
 </html>
3
  • 1
    this is a breaking change with angular 1.3.x+. You can no longer declare controllers as global objects like this, you must declare a module and declare the controller as part of the module. See this answer stackoverflow.com/questions/29149665/… Commented Jul 28, 2015 at 8:38
  • as a side note, even though that link to google apis works, I would never use angular 1.0.1 for any purpose. Commented Jul 28, 2015 at 8:41
  • @Claies this is indeed the case. I you poor this in an Answer I can give you full credit, thanks. Takeaway: don't by deadtree books they are obsolete before they are printed Commented Jul 28, 2015 at 8:47

1 Answer 1

2

Your controller cannot be global in the latest release of Angular, and, even if it should, you should try to modularise your design anyway.

Declare a module:

angular.module('myApp', [])

and register the controller onto the module:

angular.module('myApp')
   .controller('TextController', function() { /*...*/ })

Finally, add the module to the ng-app directive:

<html ng-app="myApp">
Sign up to request clarification or add additional context in comments.

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.