简体   繁体   English

角度单击时在ng-repeat中隐藏元素

[英]Angular hide an element in ng-repeat on click

I have articles listed on a page in my app, where I have an iframe and overlay over that iframe. 我的应用程序页面上列出了文章,其中有一个iframe并覆盖在该iframe上。 I want to have it that when a user clicks on an iframe overlay, that the title from that article gets hidden. 我希望当用户单击iframe叠加层时,该文章的标题被隐藏。 Not sure how to achieve that in ng-repeat. 不确定如何在ng-repeat中实现。 This the code, where as of now, on a click event titles of all articles get hidden. 到目前为止,此代码的点击事件中所有文章的标题都被隐藏了。

<ion-item ng-repeat="article in articles" class="item-light">
    <div class="article">
        <a ng-show="article.external_media.length == 0 || article.external_media.url == ''"  href="#/main/article/{{article.id}}" nav-direction="forward" class="article-image-link">
            <img src="{{ fileServer }}/imagecache/cover/{{article.cover_image}}">
            <h1>{{ article.title.split(' ', 7).join(' ') }}</h1>
        </a>
        <div class="iframe" ng-show="article.external_media.length > 0 && article.external_media.url != ''">
            <iframe ng-src="{{article.external_media[0].url | safeUrl }} "></iframe>
            <div class="article-image-link">
                <h1 ng-hide="videoPlaying">{{ article.title.split(' ', 7).join(' ') }}</h1>
            </div>
            <div class="iframe-overlay" ng-click="playVideo()"></div>
        </div>
    </div>
</ion-item>

Controller: 控制器:

$scope.playVideo = function(videoSrc) {
    $scope.videoPlaying = true;
    $scope.videoSrc = videoSrc + "&autoplay=1";
};

I've created a JSFIDDLE (basic) to give you a head start on what you are trying to do. 我创建了一个JSFIDDLE (基本),以使您可以轻松地开始尝试进行操作。

VIEW 视图

<div ng-app="app">
  <div ng-controller="helloCnt">

    <div ng-repeat='item in data'>
      <span ng-if="item.hide === undefined || !item.hide">{{item.name}}</span>
      <div ng-if="!item.hide" ng-click="playVideo(item)">
        HIDE
      </div>
      <div ng-if="item.hide" ng-click="stopVideo(item)">
        SHOW
      </div>
      <br/>
    </div>

  </div>
</div>

CONTROLLER 控制器

var app = angular.module('app', [])
app.controller('helloCnt', function($scope) {
  $scope.data = [{
    'name': 'test'
  }, {
    'name': 'in'
  }, {
    'name': 'dummy'
  }, {
    'name': 'data'
  }]

  $scope.playVideo = function(item, videoSrc) {
    // other code
    item.hide = true;
  };
  $scope.stopVideo = function(item, videoSrc) {
    // other code
    item.hide = false;
  };
})

SEE JSFIDDLE 查看JSFIDDLE

you can also use $index for this purpose. 您也可以将$ index用于此目的。

controller 控制者

  app.controller('someController',function($scope){

   $scope.videoPlaying = [];

   $scope.playVideo = function(videoSrc,index) {

   $scope.videoPlaying[index] = true;
   $scope.videoSrc = videoSrc + "&autoplay=1";
};

   $scope.stopVideo = function(videoSrc,index) {

   $scope.videoPlaying[index] = false;
   $scope.videoSrc = videoSrc + "&autoplay=1";
};

})

HTML 的HTML

i am changing only two lines rest are same as it is 我正在更改只有两行休息是一样的

 <h1 ng-hide="videoPlaying[$index]">{{ article.title.split(' ', 7).join(' ') }}</h1>



   <div class="iframe-overlay" ng-click="playVideo($index)"></div>

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

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