Edgar
Edgar

Reputation: 503

ng-click is not working angularJS

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???

Upvotes: 0

Views: 1002

Answers (2)

user3581054
user3581054

Reputation: 123

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

Upvotes: 0

Etse
Etse

Reputation: 1245

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>

Upvotes: 5

Related Questions