嵌套ng-repeat中的角度ng-show / ng-hide问题

时间:2015-12-20 13:14:40

标签: javascript html angularjs

我是棱角分明的新手,我知道这个问题已经被问了很多次,但是我无法在这里做到。

以下是我的问题的JSBin

我想在这里完成一系列卡片(trello风格),但我无法像在trello中那样制作它。在这里点击添加卡片时,angular的编译成功将卡片添加到列表中,但我坚持隐藏添加卡片锚标签。我已经应用了一些ng-show / ng-hide但是它没有维护索引并且在ng-repeat中隐藏了其他添加卡锚(我知道它很自然,但我无法将其排序)。有人可以帮帮我吗谢谢

以下是代码:

角度代码

angular.module('app', []).controller('DemoCtrl',function($scope,$compile,$window){
    $scope.items = [];
    $scope.idx = '';
    $scope.c = {};

$scope.addNewList = function () {
if(typeof $scope.c.newList === 'undefined'){
 alert('list title blank');
 return;
}
$scope.items.push({listTitle: $scope.c.newList});
$scope.c.newList = '';
};

$scope.addNewCardToList = function(idx) {
$scope.idx = idx;
var elm = '<div class="list-card"><textarea style="overflow: hidden; word-wrap: break-word; resize: none; height: 56px;" ng-model="c.title"></textarea><input type="button" value="Add" ng-click="saveNewCardToList(idx)"><a href="javascript:void(0)" ng-click="closeCard(idx)">X</a><a href="javascript:void(0)"></a></div>';
var temp = $compile(elm)($scope);
if($window.document.getElementsByClassName("list-card").length === 0)
 angular.element(document.querySelector('#compose_'+idx)).append(temp);
};
});

HTML

<div ng-controller="DemoCtrl">
<div ng-repeat="item in items" style="display:inline-block;width:120px;">
<div>{{item.listTitle}}</div>
    <div ng-repeat="inner in item.items">{{inner}}</div>
    <div id="compose_{{$index}}"></div>
<a href="javascript:void(0)" data-ng-click="addNewCardToList($index);">Add a card...</a>
</div>
<br />
<a href="javascript:void(0)" ng-click="showInput = ! showInput">Add a list...</a>
<div ng-show="showInput">
<input type="text" placeholder="Add a list..." name="title" ng-model="c.newList">
<a href="javascript:void(0)" data-ng-click="addNewList()">Save</a>
</div>
</div>

1 个答案:

答案 0 :(得分:1)

即使你的JSBin部分工作(textarea下面的添加和X按钮不起作用),最好的方法是保持你的方法角度 是here

但是,这种方法似乎不像是角度方式。通常,与控制器中的DOM控制相关的任何事情都不是最佳实践。 This will be great guide for you

无论如何,我完成了另一个JSBin完全正常工作。看一看。