简体   繁体   English

在ng-repeat中使用ng-click和ng-hide / show / Angular

[英]Using ng-click and ng-hide/show inside ng-repeat, Angular

I have a basic Single Page Website using Angular. 我有一个使用Angular的基本单页网站。 I have used an ng-repeat to generate some of the content on the main page. 我已经使用ng-repeat来生成主页上的某些内容。 Each item in the repeat is an image, and I want to have an alternate view for each one, showing more detail. 重复中的每个项目都是一幅图像,我想为每个项目提供一个替代视图,以显示更多细节。 I have a basic implementation for this and the views are toggling using ng-hide/show. 我有一个基本的实现,视图使用ng-hide / show切换。 However there seems to be a slight flicker, and I suspect all of each photos view is toggling; 但是似乎有些闪烁,我怀疑每张照片的视图都在切换; as opposed to the one clicked. 而不是一个点击。

Here is the HTML: 这是HTML:

<div class="row">
    <div class="col-sm-6 col-md-4" ng-repeat='service in services'>
      <div class="thumbnail">
        <div class='image-container' ng-show="show" ng-style="{'background-image': 'url(' + service.photo + ')'}">
        </div>
        <div class='image-container alt' ng-hide='show' ng-style="{'background-image': 'url(' + service.photo + ')'}">
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In fringilla est justo, nec pellentesque ex dictum et. Etiam laoreet, justo in condimentum sodales, nisi sapien euismod dolor, vitae feugiat nulla dui eu eros. Ut efficitur vel ipsum et pellentesque. Sed placerat risus eget euismod mattis.</p>
        </div>
        <div class="caption">
          <h4>{{ service.name }}</h4>
          <button ng-click="show=!show" class='service-button'>More Info</button>
        </div>
      </div>
    </div>
  </div>

CSS: CSS:

.service-button
{
  position: absolute;
  bottom: 40px;
  right: 30px;
}

.image-container
{
  width:100%; 
  height:300px;
}

.alt
{
  opacity: 0.4;
  font-size: 1.4em;
  padding: 10px;
  color: black;
}

Controller: 控制器:

  $scope.show = true;

Is there a more efficient way of doing this? 有更有效的方法吗?

Have you tried adding a boolean show field on each service object. 您是否尝试过在每个服务对象上添加布尔显示字段。 So ng-show="service.show" and ng-click="service.show=!service.show"? 那么ng-show =“ service.show”和ng-click =“ service.show =!service.show”? That would be my suggested way of handling it 那就是我建议的处理方式

I think the flicker may be happening because you're hiding one div and then showing the other. 我认为闪烁可能正在发生,因为您要隐藏一个div,然后显示另一个。 Right now you have: 现在您有:

<div class='image-container' ng-show="show" ng-style="{'background-image': 'url(' + service.photo + ')'}">
</div>
<div class='image-container alt' ng-hide='show' ng-style="{'background-image': 'url(' + service.photo + ')'}">
   <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In fringilla est justo, nec pellentesque ex dictum et. Etiam laoreet, justo in condimentum sodales, nisi sapien euismod dolor, vitae feugiat nulla dui eu eros. Ut efficitur vel ipsum et pellentesque. Sed placerat risus eget euismod mattis.</p>
</div>

Maybe try: 也许尝试:

<div class='image-container' ng-class="{'alt': show == true}" ng-style="{'background-image': 'url(' + service.photo + ')'}">
       <p ng-show="show">Lorem ipsum dolor sit amet, consectetur adipiscing elit. In fringilla est justo, nec pellentesque ex dictum et. Etiam laoreet, justo in condimentum sodales, nisi sapien euismod dolor, vitae feugiat nulla dui eu eros. Ut efficitur vel ipsum et pellentesque. Sed placerat risus eget euismod mattis.</p>
</div>

So that you're just adding that alt class and showing the info if show == true . 这样您就可以添加alt类,并在show == true显示信息。

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

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