0

I am trying to create an angular application from scratch. I have been trying to solve this for hours now, but I couldn't make it work. All the following files are placed inside a parent folder.

index.html

<!DOCTYPE html>
<html lang="en">

<head>
  <title>Hello</title>
  <script data-require="[email protected]" data-semver="1.6.0" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.0/angular.js"></script>
  <link rel="stylesheet" href="style.css" />
  <script src="script.js"></script>
</head>

<body ng-app="app">
  <h1>Student App</h1>
  <section ng-controller="HelloController">
    <h4>Enter Student Details</h4>
    <label for="name">Name :</label>
    <input id="name" type="text" ng-model="student.name" />
    <p>Hello {{student.name}}!</p>
  </section>
  <button id="name" type="submit" ng-click="onButtonClick()">Click</button>

</body>

</html>

HelloController.js

  angular.module('app', [])
    .controller('HelloController', ["$scope", function($scope) {

    $scope.onButtonClick = function()
    {
        console.log("method invoked");
    };

}]);

It would be nice if someone could help me solve this problem I am facing.

2
  • 1
    What happens if you open the dev console and set breakpoints on onButtonClick ? Commented Mar 16, 2019 at 22:39
  • I haven't set breakpoints. I will give it a try. Commented Mar 17, 2019 at 3:44

1 Answer 1

2

Your button is out of the <section> controlled by the controller. So clicking on it calls onButtonClick() on the root scope, not on te controller's scope.

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

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.