简体   繁体   English

在单击时显示和隐藏父div内的跨度无法正常工作

[英]Show and hide spans inside parent div on click not working as expected

My markup looks like: 我的标记看起来像:

<div class="box" ng-click="listThis()" ng-show="isVisible">
 <span class="text">content ....</span>
 <span class="text2">content2 ....</span>
 <span class="text3">content3 ....</span>

My code looks like: 我的代码如下:

var spans = 2;
var i = 0;
$scope.newlist= [];

while (i < spans){
$scope.newlist.push(i+1);
i++;
}

$scope.listThis=function(){
$scope.isVisible=!$scope.isVisible;
}

However when I click on / off everything within class="box" is hidden and doesn't really take my spans value to limit, say, 2 spans are shown only. 但是,当我单击/关闭class =“ box”内的所有内容时,它们将被隐藏,并且不会真正限制我的span值,例如,仅显示2个span。

Now sure if I've missed anything out, or a better way of doing this? 现在确定我是否错过了任何东西,或者有更好的方法呢?

This is because your ng-show is on your div which contains the spans. 这是因为您的ng-show位于包含跨度的div上。 So if isVisible is false, the entire div and all its content is hidden. 因此,如果isVisible为false,则将隐藏整个div及其所有内容。 To change this you should create conditions for each span, which show/hide accordingly. 要更改此设置,您应该为每个跨度创建条件,这些条件将相应地显示/隐藏。 For example: 例如:

<div class="box">
    <span class="text" ng-click="listContentOne()" ng-show="oneIsVisible"></span>

And in your controller: 在您的控制器中:

$scope.listContentOne = function() {
    $scope.oneIsVisible = !$scope.oneIsVisible;
}

This is a very simple and basic version of what you want to achieve, but with this information you should be able to derive your own solution. 这是您想要实现的非常简单和基本的版本,但是有了这些信息,您应该能够得出自己的解决方案。

The first answer is a good answer about your issue on ng-show. 第一个答案是有关ng-show问题的一个很好的答案。

According to your comments you may want to use a ng-repeat with a filter. 根据您的评论,您可能希望将ng-repeat与过滤器一起使用。

First here is a working plunker 首先,这是一个工作的小伙子

HTML : The ng-repeat - limit is a $scope var. HTML:ng-repeat- limit是$ scope变量。 the "limitTo" filter return the n first elements of the list. “ limitTo”过滤器返回列表的前n个元素。

<div>
    <span ng-repeat="span in spans | limitTo: limit">{{span.content}}<br/></span>
</div>

JS : JS:

//Now your span elements are objects with the attribute "content"
$scope.spans = [{content:"Test1"},{content:"Test2"},{content:"Test3"}]

You can bind an input to limit with ng-model="limit" . 您可以使用ng-model="limit"将输入绑定为limit Or simply doing $scope.limit = 2 in your JS 或者只是在JS中执行$scope.limit = 2

Hope it helped. 希望能有所帮助。

An ng-repeat is probably more what you're looking for. ng-repeat可能更多是您想要的。 Assuming you have dynamic content for your spans setup like so: 假设您具有跨度设置的动态内容,如下所示:

$scope.spans = [
  {content: 'content1'},
  {content: 'content2'},
  {content: 'content3'}
]

Then your html should be like this: 然后您的html应该是这样的:

<span ng-repeat="span in spans">{{span.content}}</span>

From there you have a few options depending on what you want to do. 从那里开始,您可以根据自己的选择进行选择。 If your spans are sorted and you just want to show a certain amount, add $scope.limit somewhere and use limitTo : 如果您的跨度已排序并且只想显示一定数量,请在某处添加$scope.limit并使用limitTo

<span ng-repeat="span in spans | limitTo: limit">{{span.content}}</span>

Otherwise put another variable on the span objects called visible , set that to true or false, and then do 否则,将另一个变量放在跨度对象上,称为visible ,将其设置为true或false,然后执行

<span ng-repeat="span in spans" ng-show="span.visible">{{span.content}}</span>

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

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