【问题标题】:How to use collapse in a directive?如何在指令中使用折叠?
【发布时间】:2015-03-04 00:20:24
【问题描述】:

我有这部分代码,其中第二个 <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 中的Cancel 按钮,&lt;tr&gt; 应该消失。

【问题讨论】:

  • 发布employee-form指令代码。
  • app.directive('employeeForm', function () { return { restrict: 'E', templateUrl: '/Scripts/Employee/Form.html' } });
  • 你试过&lt;button type="button" class="btn btn-default" value="Cancel" ng-click="isCollapsed = false" /&gt; 吗?

标签: angularjs twitter-bootstrap angularjs-directive collapse


【解决方案1】:

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

【讨论】:

  • 令人惊讶的是,这在 Plunker 中有效,但在我的 Visual Studio 13 中无效。:( 我在代码中看到的唯一区别是 ng-hide="isCollapsed" 而不是 collapse="isCollapsed"。这会有所不同吗?
  • collapsed="{{isCollapsed}}" |我不知道它是否有效,你检查一下。
  • 顺便说一句,想知道如何在我们的代码中隐藏表格中的其他表单...例如,您一次只能编辑一个条目;一旦您单击其他数据行的编辑,已打开的其他数据行的可编辑形式应隐藏。基本上,使用表格行和折叠功能的手风琴。
  • 太棒了,@mia!惊人的! :)
猜你喜欢
  • 2014-02-10
  • 1970-01-01
  • 2013-07-11
  • 1970-01-01
  • 1970-01-01
  • 2022-01-03
  • 2022-01-11
  • 2019-04-01
  • 2012-03-26
相关资源
最近更新 更多