如何在指令中使用崩溃?

时间:2015-01-06 10:39:31

标签: angularjs twitter-bootstrap angularjs-directive collapse

我有这部分代码,其中第二个<tr>仅出现在编辑点击上,并且出现了一个我正在使用指令显示的表单。

 <table class="table">
            <tbody ng-repeat="Emp in Employees">
                <tr>
                    ...
                    <td>
                        <input type="button" class="btn btn-default" value="Edit" ng-click="isCollapsed = !isCollapsed" />
                   </td>
                </tr>
                <tr collapse="isCollapsed">
                    <td>
                        <div>
                            <employee-form></employee-form>
                        </div>
                    </td>
                </tr>
            </tbody>
        </table>

现在我的employeeForm指令中也有一个按钮,点击它时应该使这个表格消失(isCollapsed = true)。但不知何故,这是行不通的。

员工表格的标记:

<form>
    ...
    <button type="button" class="btn btn-default" value="Cancel" ng-click="isCollapsed = true" />
</form>

JS的部分是:

$scope.isCollapsed = true;

所以我想要的是点击employeeForm中的“取消”按钮<tr>应该消失。

1 个答案:

答案 0 :(得分:0)

View in Plunker

我希望它可以帮到你:.. HTML:

<div ng-app="myapp" ng-controller="myctrl" id="id01">

<table>
    <tbody ng-repeat="emp in Employees">
    <tr>
        <td>
            <p>{{emp}}<input type="button" value="edit" ng-click="setshowindex($index)"/></p>
        </td>
    </tr>
    <tr ng-class="{'show':$index==showing,'hide':$index!==showing}">
        <td>
            <employee-form></employee-form>
        </td>
    </tr>
    </tbody>
</table>

脚本:

angular.module('myapp',[])
        .controller('myctrl',function($scope){
            //$scope.collapsed = true;
            $scope.Employees = ['abc','xyz','klm'];
            $scope.showing = -1;
            $scope.setshowindex = function (i) {
                $scope.showing = -1;
                $scope.showing = i;
            };
        })
        .directive('employeeForm',function(){
            return {
                restrict: 'E',
                replace:true,
                template: '<form><input type="text" ng-value="emp" /><button ng-click="setshowindex(-1)">Cancel</button></form>'
            };
        });

CSS:

tr.show{
            display: table-row;
        }
tr.hide{
            display: none;
        }