ng-bind如何创建表达式

时间:2017-11-30 19:38:06

标签: angularjs ng-bind

我在这里有一个代码分支:

<tr ng-repeat="ticket in allTickets | orderBy:sortType:sortReverse|filter:searchTicket track by $index">
                <td>{{ticket.id}}</td>
                <td><a th:href="@{overview/{{ticket.id}}}">{{ticket.name}}</a></td>
                <td>{{ticket.desiredResolutionDate}}</td>
                <td>{{ticket.urgency}}</td>
                <td>{{ticket.state}}</td>
                <td>
                    <div class="btn-group">
                        <button type="button" class="btn btn-primary" ng-bind="action+$index"></button>
                        <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
                            <span class="caret"></span>
                        </button>
                        <ul class="dropdown-menu" role="menu">
                            <li><a href="" ng-click="changeAction('approve',$index)">Approve</a></li>
                            <li><a href="" ng-click="changeAction('decline',$index)">Decline</a></li>
                            <li><a href="" ng-click="changeAction('submit',$index)">Submit</a></li>
                            <li><a href="" ng-click="changeAction('cancel',$index)">Cancel</a></li>
                            <li><a href="" ng-click="changeAction('assign',$index)">Assign to me</a></li>
                            <li><a href="" ng-click="changeAction('done',$index)">Done</a></li>
                            <li><a href="" ng-click="changeAction('leavefeedback',$index)">Leave Feedback</a></li>
                            <li><a href="" ng-click="changeAction('viewfeedback',$index)">View Feedback</a></li>
                        </ul>
                    </div>
                </td>
            </tr>

控制器功能:

$scope.changeAction = function (action, index) {
        if (index == 0) {
            $scope.action0 = action;
        }
        else if (index == 1) {
            $scope.action1 = action;
        } else if (index == 2) {
            $scope.action2 = action;
        } else if (index == 3) {
            $scope.action3 = action;
        } else if (index == 4) {
            $scope.action4 = action;
        } else if (index == 5) {
            $scope.action5 = action;
        } else if (index == 6) {
            $scope.action6 = action;
        } else if (index == 7) {
            $scope.action7 = action;
        }
    }

我想在点击下拉项目时更改我的按钮值。我想将所有按钮值绑定到像'action'+ $ index这样的表达式,因此我希望将第一个按钮绑定到'action0',将第二个按钮绑定到'action1'等等。我的示例不起作用。< / p>

1 个答案:

答案 0 :(得分:0)

要使用“动态”ng-bind值,您可以使用它来制作action0,action1等。

   <button type="button" class="btn btn-primary" ng-bind="{{ 'action' + $index }}"></button>