2014-10-30 105 views
0

我使用語義UI來格式化我的網站與AngularJS。我的控制器已經建立並運行良好。但它只是不喜歡我的這部分代碼:ng-click不工作 - TypeError:undefined不是函數

<div class="ui piled feed segment"> 
     <h2 class="ui header"> 
      Comments 
     </h2> 
     <div class="event" ng-repeat = "comment in comments | reverse"> 
      <div class="ui green horizontal label"> 
       {{comment.user}} 
      </div> 
      <div class="content"> 
       <div class="summary" ng-click="doSomething()"> 
        {{ comment.content }} 
       </div> 
      </div> 
     </div> 
    </div> 

這裏是AngularJS代碼:

$scope.doSomething = function(){ 
    alert('blah'); 
}; 

基本上,我想執行DoSomething的()被點擊的內容時。但由於某些原因,它不能正常工作,並提供了以下錯誤:

TypeError: undefined is not a function 
    at http://localhost:3000/js/lib/angular.min.js:65:98 
    at http://localhost:3000/js/lib/angular.min.js:72:222 
    at http://localhost:3000/js/lib/angular.min.js:144:140 
    at Object.e.$eval (http://localhost:3000/js/lib/angular.min.js:88:347) 
    at Object.e.$apply (http://localhost:3000/js/lib/angular.min.js:88:454) 
    at HTMLAnchorElement.<anonymous> (http://localhost:3000/js/lib/angular.min.js:144:122) 
    at HTMLAnchorElement.n.event.dispatch (http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js:3:8066) 
    at HTMLAnchorElement.r.handle (http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js:3:4774) 

我不是很確定這個錯誤是什麼意思,因爲通常是「不確定」的意思有什麼錯誤的函數的定義,但在這裏,我發現沒有什麼錯。我有ng-click在ng-repeat裏面,在這個應用程序中的任何地方,它工作正常。是因爲語義UI嗎?

+0

它不會這樣的,首先你要在你的HTML – 2014-10-30 07:06:54

+0

哦分配NG-的應用程序,這只是我的代碼片段。應用程序,模塊,控制器,一切都很好定義。 – 2014-10-30 07:08:13

+0

你可以試一下小提琴演示嗎? – 2014-10-30 07:09:17

回答

0

試試這個..

<body ng-app="myApp"> 
     <div class="ui piled feed segment" ng-controller="myController as ctrl"> 
      <div class="summary" ng-click="doSomething()"> 
       {{name}} 
      </div> 
     </div> 

     <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script> 
     <script> 
     var app = angular.module('myApp',[]); 
     app.controller('myController',function($scope){ 
      $scope.name = "Hi I am a new user Click me!"; 
      $scope.doSomething = function(){ 
       alert('blah'); 
       $scope.msg = 'clicked'; 
      }; 
     }) 
     </script> 
    </body> 
0

我相信這是與從納克重複的內部調用它。我忘記了細節,但這是某種範圍的問題。

代替$ scope.doSomething,儘量

$scope.myObject = {}; 
$scope.myObject.doSomething = function(){//stuff}; 

然後在HTML調用myObject.doSomething();

然後,如果我對我的假設是正確的,並且實際工作,你可能想看看這篇關於有助於避免這種類型範圍問題的角度實踐的文章。

http://www.technofattie.com/2014/03/21/five-guidelines-for-avoiding-scope-soup-in-angular.html

相關問題