角NG单击DIV不工作 [英] Angular ng-click not working in div

查看:139
本文介绍了角NG单击DIV不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我已经困在这里了一段时间,我敢肯定这件事情比较愚蠢的。

Ok, I've been stuck here for a while, and I'm sure it's something relatively dumb

<一个href=\"http://plnkr.co/edit/YcBnbE5VCU5rizkDWreS?p=$p$pview\">http://plnkr.co/edit/YcBnbE5VCU5rizkDWreS?p=$p$pview

<head>
    <link href="http://twitter.github.com/bootstrap/assets/css/bootstrap.css" rel="stylesheet" type="text/css" />
    <link href="http://twitter.github.com/bootstrap/assets/css/bootstrap-responsive.css" rel="stylesheet" type="text/css" />
    <script src="http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.0.3/angular.min.js"></script>

    <script >
        function myCtrl($scope,  $window) {
            $scope.vm = {};
            $scope.vm.Courses = [
              { Id: 1, Name: "Course 1"},
              { Id: 2, Name: "Course 2"}
              ];
            $scope.OpenCourse = function(courseId) {
                $window.alert("Called " + courseId);
            }
        }
    </script>
</head>
<body ng-controller="myCtrl">
    <div>  
        <div ng-repeat="course in vm.Courses" ng-click="vm.OpenCourse(course.Id)">
            <div>
                <div>
                    <label>{{course.Name}}</label>
                </div>
            </div>
        </div>
    </div>
</body>

为什么不NG-请点击此处射击?看来,这个问题是问了很多,但没有一个答案似乎帮助。它也像移动DIV出来的重复使得它的工作,但同样,我不知道为什么。

Why isn't ng-click firing here? It seems that this question is asked a lot, but none of the answers seem to help. It also looks like moving the div out of the repeat makes it work, but again, I'm not sure why.

感谢

推荐答案

删除 VM。

结果:

<div ng-repeat="course in vm.Courses" ng-click="OpenCourse(course.Id)">

为什么呢?因为你设置为 $范围一切都变得可在局部的,那么你只需要调用它。

Why?, because everything you set to $scope becomes available on the partial, then you just have to call it.

这篇关于角NG单击DIV不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆