ng-click不工作angularJS

时间:2022-10-22 09:15:37

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

我正在学习angularJS,我正在使用“使用Bootstrap和AngularJs学习Web开发”这本书,我正在尝试为按钮创建一个ng-click功能。但是当我点击它时没有任何反应。这是我的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???

我究竟做错了什么???

2 个解决方案

#1


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.

您需要创建一个模块,并在模块上创建控制器。然后,您需要告诉angular引导模块作为应用程序的根目录(使用ng-app指令)。我建议你再看看你所遵循的那个问题。

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>

#2


0  

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

注入$ window,然后调用它。你没有那个圆顶

#1


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.

您需要创建一个模块,并在模块上创建控制器。然后,您需要告诉angular引导模块作为应用程序的根目录(使用ng-app指令)。我建议你再看看你所遵循的那个问题。

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>

#2


0  

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

注入$ window,然后调用它。你没有那个圆顶