AngularJS根据ng-repeat中的条件设置按钮文本

Jar*_*ier 17 angularjs

我正在尝试通过转换一段有效的jQuery/AJAX代码来学习Angular.它是表格内的活动查看器,基本上根据条件显示带有"标记读取"或"标记未读"按钮的消息.我在设置按钮文本时遇到问题activity.seen == 0.

// html
<div class="table-responsive" ng-controller="Activity">
    <table class="table table-bordered table-striped table-hover">
        <tbody>
            <tr ng-repeat="activity in activities" ng-class="{ 'activity-read': activity.seen == 1 }">
                <td><button class="btn btn-default"><span ng-bind="buttonText"></span></button></td>
                <td>{{ activity.content }}</td>
            </tr>
    </table>
</div>

// controller
function Activity($scope, $http, $templateCache)
{
    $http.get('/activity/get').success(function(response, status)
    {
        $scope.activities = response;

        angular.forEach($scope.activities, function(activity)
        {
            $scope.buttonText = activity.seen == 0 ? 'Mark Read' : 'Mark Unread';
        });
    });
}
Run Code Online (Sandbox Code Playgroud)

这只是设置buttonText为activity.seen条件的最后一个值.那么如何在初始加载时动态地将正确的文本绑定到每一行的按钮.

这应该具有双向绑定我认为因为我还需要在点击按钮文本之后更新按钮文本,activity.seen其中新值将通过更新方法发布$http.

Gru*_*nny 62

我认为这应该做你想要的:

    <button ng-click='activity.seen = !activity.seen'>
        <span>{{activity.seen ? 'Mark unread' : 'Mark read'}}</span>
    </button>
Run Code Online (Sandbox Code Playgroud)

Fiddle