[英]ng-click is not working angularJS
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. 我正在学习angularJS,并且正在使用《学习用Bootstrap和AngularJs进行Web开发》这本书,并试图为按钮创建ng-click功能。 but nothing happens when i click it. 但是当我单击它时什么也没有发生。 this is my controller.js... 这是我的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??? 我究竟做错了什么???
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). 然后,您需要告诉angular将模块作为应用程序的根进行引导(使用ng-app指令)。 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>
Inject $window, and call it. 注入$ window,然后调用它。 You have not dome that 你没有那个圆顶
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.