3

Here is my code, Actually when I click on function but not showing alert and not getting error in console. So please tell me how to do.

HTML Code

 <li class="has-megamenu" ng-click="homepage()" ><a ><span >Home</span></a>
 </li>

app.js

app.controller('myController', function($scope){
$scope.homepage = function(){
    alert('hi');
}});

2 Answers 2

6

There can be two reasons

1- Not using ng-app directive in the root element of your application.

2- You have not defined ng-controller properly.

Please follow following working code.

<html>
<head>
  <link rel="stylesheet" href="style.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.5/angular.js"></script>

  <script>
    var app = angular.module('app', []);
    app.controller('myController', function($scope) {
      $scope.homepage = function() {
        alert('hi');
      }
    });
  </script>
</head>

<body ng-app="app">
  <div ng-controller="myController">
    <ul>
      <li class="has-megamenu" ng-click="homepage()"><a><span >Home</span></a>
      </li>
    </ul>
  </div>

</body>

</html>

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

Comments

3

First You have to add ng-app = "app"

Second You have to add ng-controller = "myController"

Final Please check the updated code:

var app = angular.module("app", []);
app.controller('myController', function($scope){
$scope.homepage = function(){
    alert('hi');
}});
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
</head>
  <body ng-app = "app">
    <div ng-controller = "myController">
      <li class="has-megamenu" ng-click="homepage()" ><a ><span >Home</span></a>
       </li>
     <div>
   </body>
 </html>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.