0

I'm reading AngularJS from O'REILLY, and i tried to see how angular works with an example but i can make it functional:

The hello.html :

<html ng-app>
  <head>
    <script src="angular.js"></script>
    <script src="controllers.js"></script>
  </head>
  <body>
    <div ng-controller="HelloController">
      <p>{{ greeting.text }}, World</p>
    </div>
  </body>
</html>

and the logic within the controllers.js :

function HelloController($scope) {
  $scope.greeting = { text: 'Hello' };
}

but when i display the hello.html on the browser, i can see {{ greeting.text }}, Hello.

What is wrong here?

Vercryger
  • 620
  • 7
  • 17
  • 3
    If you're using version 1.3+, you cannot by default use global functions as controllers. You should check your console for errors – Phil Feb 09 '15 at 22:01
  • possible duplicate of [Controller not a function, got undefined, while defining controllers globally](http://stackoverflow.com/questions/25111831/controller-not-a-function-got-undefined-while-defining-controllers-globally) – Phil Feb 09 '15 at 22:02

1 Answers1

1

You never defined a controller, you just defined a function that happened to have "controller" in the name.

Try initializing the app properly:

var myApp = angular.module('myApp',[]);

myApp.controller('HelloController', ['$scope', function($scope) {
  $scope.greeting = {text: 'Hello'};
}]);

https://docs.angularjs.org/guide/controller

mz3
  • 1,314
  • 11
  • 27
  • 1
    That's not his issue, he should be able to do it without having to define it the way you have. – yangli-io Feb 09 '15 at 22:03
  • 1
    @YangLi not in 1.3 (by default). In any case, this question has already been answered (assuming this is the actual problem) – Phil Feb 09 '15 at 22:06