简体   繁体   English

ng-click在指令中的表格行上不起作用

[英]ng-click not working on table row in a directive

This is a follow up to this question: How do I get my model to my Directive? 这是此问题的后续措施: 如何将我的模型添加到指令中?

The directive is displaying the data, but the ng-click does nothing. 该指令正在显示数据,但是ng-click不会执行任何操作。

my partial page looks like this now: 我的部分页面现在看起来像这样:

<div>selected user:{{selectedUser.UserName}} </div>

<div user-list data-users="users"></div>

and my directive now looks like this: 现在我的指令如下所示:

tsUui.directive('userList', function(){
    return{
        restrict: 'A',
        template: '<table>'+
                    '<tr>'+
                        '<th>User Name</th>'+
                        '<th>First Name</th>'+
                        '<th>Last Name</th>'+
                        '<th>Email Address</th>'+
                    '</tr>'+
                    '<tr ng-repeat="user in users" ng-click="selectUser(user)">'+
                        '<td>{{user.UserName}}</td>'+
                        '<td>{{user.FirstName}}</td>'+
                        '<td>{{user.LastName}}</td>'+
                        '<td>{{user.Email}}</td>'+
                    '</tr>'+
                '</table>',
        scope:{
                selectedUser: '=',
                users: '='
        },
        link: function (scope, elem, attrs){
            scope.selectUser = function(user){
                console.log("hi");
                selectedUser=user;
            };
        }
    }
});

the data is displaying properly, but when I click on a row nothing happens: no console logs, the selected user doesn't change...nothing. 数据正确显示,但是当我单击一行时,什么都没有发生:没有控制台日志,所选用户没有更改...什么都没有。 What am i doing wrong? 我究竟做错了什么?

Edit: the console is logging it, but the selected user binding doesn't change... 编辑:控制台正在记录它,但所选的用户绑定不会更改...

Apparently you have to do it like this: 显然,您必须这样做:

scope.selectedUser=user;

also I changed the partial to this: 我也改变了部分:

<div>selected user:{{selection.UserName}} </div>

<div user-list data-users="users" selected-user="selection"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM