【问题标题】:ng-click doesn't invoke method inside angularJs controllerng-click 不会调用 angularJs 控制器中的方法
【发布时间】:2023-03-24 07:53:01
【问题描述】:

我正在尝试从头开始创建一个角度应用程序。我已经尝试解决这个问题几个小时了,但我无法让它发挥作用。 以下所有文件都放在一个父文件夹中。

index.html

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

<head>
  <title>Hello</title>
  <script data-require="angular.js@1.6.0" 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");
    };

}]);

如果有人能帮我解决我面临的这个问题,那就太好了。

【问题讨论】:

  • 如果您打开开发控制台并在onButtonClick 上设置断点会发生什么?
  • 我没有设置断点。我会试一试的。

标签: angularjs angularjs-ng-click angular-controller


【解决方案1】:

您的按钮不在控制器控制的&lt;section&gt; 中。所以点击它会在根范围内调用onButtonClick(),而不是在控制器的范围内。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-06
    • 2015-05-31
    • 2018-03-21
    • 2017-03-23
    • 2016-05-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多