0

我正在制作一个简单的邮件界面,我的设置看起来像......

                <tbody ng-controller="sentboxCtrl">
                    <tr ng-repeat="message in sent">
                        <td><input type="checkbox" value="{| message.id |}"></td>        
                        <td><a href="#" value="{| message.id |}" class="view-message-link">{| message['subject'] |}</a></td>
                        <td>

                            <img class="attachement" value="{| message.id |}" ng-if="message.attachment" src="{| attachmentIcon |}" width="20" height="20" alt=""></td>
                        <td>{| message.name |}</td>        
                        <td>{| message.created |}</td>
                        <td><a href="#" value="{| message.id |}" class="flagMessage"><img value="{| message.id |}" src="{| flagIcon |}" width="20" height="20" alt=""></a></td>
                        <td><a href="#" value="{| message.id |}" class="deleteMessage" ng-click="delete({| message |})"><img value="{| message.id |}" src="{| deleteIcon |}" width="20" height="20" alt=""></a></td>
                    </tr>   
                 </tbody> 

我配置控制器的代码如下:

var mailApp = angular.module('mailApp',[]).config(function($interpolateProvider){
        $interpolateProvider.startSymbol('{|');
        $interpolateProvider.endSymbol('|}');
    }
);

var sentboxCtrl = function ($scope){

    $scope.sent = initiallySent;
    $scope.deleteIcon = deleteIconSource;
    $scope.flagIcon = flagIconSource;
    $scope.attachmentIcon = attachmentIconSource;

    $scope.delete = function(element)
    {
        delete element;
    }

};

mailApp.controller('sentboxCtrl',sentboxCtrl);

现在,由于我有一个 ng-repeat 属性可以为消息传递平台生成我的 dom,我想绑定事件 ng-click 以触发该行的删除。一个例子是:

<td><a href="#" ng-click="delete(this)" value="{| message.id |}" class="deleteMessage" ng-click="delete({| message |})"><img value="{| message.id |}" src="{| deleteIcon |}" width="20" height="20" alt=""></a></td>

是否可以在不编写太多样板代码的情况下做到这一点?

谢谢

4

1 回答 1

1

在您的控制器中:

$scope.delete = function(i) {
   $scope.sent.splice(i, 1);
}

HTML

<a href="#" value="{| message.id |}" class="deleteMessage" ng-click="delete($index)"><img value="{| message.id |}" src="{| deleteIcon |}" width="20" height="20" alt=""></a>
于 2013-10-15T22:06:51.120 回答