Angular ng-click 在 div 中不起作用 [英] Angular ng-click not working in div

查看:30
本文介绍了Angular ng-click 在 div 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我已经被困在这里一段时间了,我确定这是一个相对愚蠢的事情

http://plnkr.co/edit/YcBnbE5VCU5rizkDWreS?p=preview

<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><脚本>函数 myCtrl($scope, $window) {$scope.vm = {};$scope.vm.Courses = [{ ID:1,名称:课程 1"},{ ID:2,名称:课程 2"}];$scope.OpenCourse = function(courseId) {$window.alert("调用" + courseId);}}<body ng-controller="myCtrl"><div><div ng-repeat="vm.Courses 中的课程" ng-click="vm.OpenCourse(course.Id)"><div><div><label>{{course.Name}}</label>

为什么 ng-click 没有在这里触发?似乎这个问题被问了很多,但似乎没有一个答案有帮助.看起来将 div 从重复中移出也可以使其工作,但同样,我不确定为什么.

谢谢

解决方案

移除 vm.

结果:

为什么?,因为您设置为 $scope 的所有内容都可以在部分上使用,那么您只需调用它即可.

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

http://plnkr.co/edit/YcBnbE5VCU5rizkDWreS?p=preview

<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>

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.

Thanks

解决方案

Remove vm.

Result:

<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.

这篇关于Angular ng-click 在 div 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆