0

我正在使用语义 UI 来使用 AngularJS 格式化我的网站。我的控制器已设置好并且运行良好。但它只是不喜欢我的代码的这一部分:

    <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-repeat 里面有 ng-click,它工作正常。是因为语义 UI 吗?

4

2 回答 2

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>
于 2014-10-30T07:21:26.350 回答
0

我相信这与从 ng-repeat 内部调用它有关。我忘记了细节,但这是某种范围界定问题。

而不是 $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

于 2014-11-06T17:11:09.417 回答