ng 单击“对指令中的表行不起作用”

ng-click not working on table row in a directive

本文关键字:不起作用 单击 指令 ng      更新时间:2023-09-26

这是这个问题的后续问题:如何让我的模型符合我的指令?

指令正在显示数据,但 ng-click 不执行任何操作。

我的部分页面现在如下所示:

<div>selected user:{{selectedUser.UserName}} </div>
<div user-list data-users="users"></div>

我的指令现在看起来像这样:

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;
            };
        }
    }
});

数据显示正常,但是当我单击一行时没有任何反应:没有控制台日志,所选用户不会更改...无。 我做错了什么?

编辑:控制台正在记录它,但选定的用户绑定不会更改...

显然你必须这样做:

scope.selectedUser=user;

我也把部分改成了这个:

<div>selected user:{{selection.UserName}} </div>
<div user-list data-users="users" selected-user="selection"></div>