0

I am learning angularJS and i am using the book, "Learning Web Development with Bootstrap and AngularJs" and i am trying to create a ng-click functionality to a button. but nothing happens when i click it. this is my controller.js...

function AppCtrl($scope){
$scope.clickHandler = function(){
    window.alert('Clicked!');
   };
}

this is my view...

<html ng-controller="AppCtrl">
<head>
    <meta charset="utf-8">
    <title>Contacts Manager</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <script type="text/javascript" src="js/angular.min.js"></script>
    <script src="js/jquery-2.1.4.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script type="text/javascript" src="js/controller.js"></script>

</head>
<body>
        <nav class="navbar navbar-default" role="navigation">
    <div class="navbar-header">
    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#nav-toggle">
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
    </button>
    <a class="navbar-brand" href="/">Contacts Manager</a>
    </div>
        <div class="collapse navbar-collapse" id="nav-toggle">
            <ul class="nav navbar-nav">
                <li class="active"><a href="/">Browse</a></li>
                <li><a href="/add">Add Contact</a></li>
            </ul>
        <form class="navbar-form navbar-right" role="search">
            <input type="text" class="form-control" placeholder="Search">
        </form>
        </div>
    </nav>
   <div class="col-sm-8">
  <button ng-click="clickHandler()">Click Me</button>
     </div>
  </body>
 </html>

what am i doing wrong???

Edgar
  • 543
  • 10
  • 20

2 Answers2

5

You need to create a module, and make the controller on the module. You then need to tell angular to bootstrap the module as the root of the application (using ng-app directive). I would reccomend taking another look at the exaple you are following.

Here's a short snippet setting up a button with a click-handler:

angular.module('myApp', [])
  .controller('AppCtrl', function($scope){
    $scope.clickHandler = function(){
      window.alert('Clicked!');
   };
  }
);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="myApp" ng-controller="AppCtrl">
  <button ng-click="clickHandler()">Click Me</button>
</div>
Etse
  • 1,235
  • 1
  • 15
  • 28
  • the above works but the word angular.module is not defined; please fix or add /* global angular */ but it works. and why do you have to modulize it? is this new ? – Edgar Dec 14 '15 at 01:26
  • Yeah, global controller-functions has been removed. Take a look at the comment by pankaj parkar for Møre info. – Etse Dec 14 '15 at 07:22
0

Inject $window, and call it. You have not dome that

user3581054
  • 125
  • 12