2016-01-18 89 views
0

我想單擊我的按鈕時調用一個函數。這段代碼應該可以工作,但它不會觸發我在控制器中定義的函數。AngularJS - ng-click不會觸發我的控制器功能

代碼編譯正常,如控制檯顯示,任何想法?
謝謝!

這是我的代碼:

<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

我想你只需要使用select()而不是navCtrl.select() –

回答

3

你並不需要在你的HTML指定'navCtrl'。只需撥打'ng-click=select()'即可。如果您使用controller-as='navCtrl',則需要使用navCtrl.select

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> 

希望工程!