简体   繁体   English

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

[英]Angular ng-show/ng-hide issue in nested ng-repeat

I'm new to angular and I know this question has already been asked so many times but I'm not able to make it here. 我是棱角新手,我知道这个问题已经被问过很多次了,但我无法在这里提出。

Here is the JSBin for my problem: 这是我的问题的JSBin

What I'm trying to accomplish here a list of cards (trello style) but I'm not able to make it as it does in trello. 我要在此处完成的卡片清单(trello样式),但我无法像trello那样做到。 Here when clicking add card, angular's compile successfully add the card to list but I'm stuck to hide add card anchor tag then. 在这里,单击添加卡片时,angular的编译成功地将卡片添加到列表中,但是我被困于隐藏添加卡片锚标签。 I've applied some ng-show/ng-hide but then it doesn't maintain the index and hides other add card anchor in the ng-repeat (I know its natural but I'm not able to sort it out). 我应用了一些ng-show / ng-hide,但是它不维护索引,并且在ng-repeat中隐藏了其他添加卡锚(我知道它是自然的,但我无法对其进行整理)。 Can somebody please help me here. 有人可以在这里帮我吗。 Thanks 谢谢

Here is the code as well: 这也是代码:

Angular code : 角度代码

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 : 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>

Even if your JSBin is working partially (Add and X button below textarea are not working), best solution with keeping your approach way about angular is here 即使您的JSBin处于部分工作状态( textarea下面的“添加”和“ X”按钮不起作用),也可以在此处找到保持角度的最佳方法。

BUT, this approach doesn't seems like angular-way. 但是,这种方法似乎不是有角度的。 Commonly, anything related with DOM controlling in controller is not best practice. 通常,与控制器中的DOM控制相关的任何事情都不是最佳实践。 This will be great guide for you . 这将为您提供指导

Anyway, I completed another JSBin just working fine entirely. 无论如何,我完成了另一个JSBin ,但效果很好。 Take a look. 看一看。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM