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

3
  • Did you check if any script errors generated in browser? Also you need to inject window as dependency since you are calling alert(...) on it? Commented Dec 13, 2015 at 16:19
  • How is the code controller, and what is the module ? Commented Dec 13, 2015 at 16:36
  • 1
    Here stackoverflow.com/a/28728380/2435473 you could find exactly what you are getting wrong Commented Dec 13, 2015 at 16:37

2 Answers 2

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>

Sign up to request clarification or add additional context in comments.

2 Comments

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 ?
Yeah, global controller-functions has been removed. Take a look at the comment by pankaj parkar for Møre info.
0

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

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.