Learning basics of Angular - example not working

34 views
Skip to first unread message

W Smith

unread,
Jan 15, 2017, 5:33:28 PM1/15/17
to Angular
Hi everyone, I am using this guide: http://www.angularjsbook.com/angular-basics/chapters/controllers/
to learn about controllers and general angular javascript with very basic javascript knowledge already. I am putting to this code together apparently the wrong way and it is not running and displaying the message as it should. Could you please tell me what I am doing wrong?

Here's my code:

<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.js">


<body ng-app="app">
  angular.module('app', []);
  angular.module('app').config(['$controllerProvider', function($controllerProvider) {
    $controllerProvider.allowGlobals();
  }]);
  <p ng-controller="EmptyController">

  </p>

  function MessageController() {
  this.message = "This is a model.";
  }

  <p ng-controller="MessageController as controller">
  {{controller.message}}
  </p>

</body>
</script>

</html>

Sander Elias

unread,
Jan 16, 2017, 12:11:26 AM1/16/17
to Angular
Hi W,

If you put script's inside your page, you need to put it inside a script tag.  like this:

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.js"></script>
<body ng-app="app">

 
<script>

    angular
.module('app', []);
    angular
.module('app').config(['$controllerProvider', function($controllerProvider) {
      $controllerProvider
.allowGlobals();
   
}]);

 
</script>

 
<!--
  <p ng-controller="EmptyController">
   commented out, because you must define a controller!
  </p>
   -->


 
<script>

   
function MessageController() {
     
this.message = "This is a model.";
   
}

 
</script>


 
<p ng-controller="MessageController as controller">
    {{controller.message}}
 
</p>


</body>
</html>


The samples that are in the page, are live-editable! So if you want to play with them, you can edit them in the book itself. (The book mentions that the JS parts are in separately loaded JS files, but glosses a bit over it really)
As a side note, while you are still learning, don't use global controllers like that. I did read a small part of the book, and it seems to be a bit outdated, while it's not wrong what is in there, quite a couple of things in there are now looked upon a tad different. If you are done with this book, you really want to read the styleguide

W Smith

unread,
Jan 22, 2017, 7:16:47 PM1/22/17
to Angular
Mr. Elias,

I appreciate your help! Thanks and I have already stopped with that book. I will look into the one you suggested.

Thanks again,
Reply all
Reply to author
Forward
0 new messages