我正在尝试在单击按钮时调用函数。此代码应该可以工作,但它不会触发我在控制器中定义的函数。
代码编译好在控制台中显示,任何想法?
谢谢!
这是我的代码:
<html lang="en" ng-app="myApp" >
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Ristorante Con Fusion</title>
<!-- Bootstrap -->
<script src="js/jquery-1.12.0.min.js"></script>
<script src="js/angular.js"></script>
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/mystyles.css" rel="stylesheet">
<script >
var app = angular.module('myApp', []);
app.controller('navCtrl', function($scope) {
$scope.select=function () {
alert('hi');
};
});
</script>
</head>
<body>
<nav id="mynav"class= "navbar navbar-inverse " role="navigation" ng-controller="navCtrl" >
<div class="container">
<ul class="nav navbar-nav color-white ">
<li >
<a id="home" ng-click="navCtrl.select()"href="#" >home</a></li>
<li >
<a id="1" href="#" ng-click="navCtrl.select() ">About</a></li>
<li >
<a id="2" href="#" ng-click="navCtrl.select() ">Contact</a></li>
</ul>
</div>
</nav>
</body>
答案 0 :(得分:3)
您不需要在HTML中指定'navCtrl'
。只需致电'ng-click=select()'
即可。如果您使用过controller-as='navCtrl'
,那么您需要使用navCtrl.select
。
答案 1 :(得分:0)
只需修改如下:
<ul class="nav navbar-nav color-white ">
<li >
<a id="home" ng-click="select()">home</a></li>
<li >
<a id="1" href="#" ng-click="select()">About</a></li>
<li >
<a id="2" href="#" ng-click="select()">Contact</a></li>
</ul>
希望它有效。!