2014-10-30 104 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

相关问题