I'm trying to hide an input, I do that in this way:

我试图隐藏输入,我这样做:

Javascript:

使用Javascript:

$scope.show= true;
$scope.showOrNot= function () {
    $scope.show= !$scope.show;
}

In my html:

在我的HTML中:

<div class="item" ng-click="showOrNot()">
     <p><b>blablabla</b></p>
</div>
<li class="item" ng-hide="show">
    <input type="text">
</li>

The showOrNot function is called when I click in the div before the specific input.

当我在特定输入之前单击div时调用showOrNot函数。

If I have 20 inputs in my page, I have to write in my javascript 20 times same functions. Have you an idea how to resolve more efficiently?

如果我的页面中有20个输入,我必须在我的javascript中写入20次相同的函数。您是否知道如何更有效地解决问题?

3 个解决方案

#1


4

You could pass a key to the showOrNot function and map the key to a show/hide object on the scope:

您可以将密钥传递给showOrNot函数,并将密钥映射到范围上的显示/隐藏对象:

  angular.module('app', [])
  .controller('exampleController', function($scope) {

    $scope.show = {};
    $scope.showOrNot = function(key) {
      $scope.show[key] = !$scope.show[key];
    };

  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>

<div ng-app="app">

  <div ng-controller="exampleController">

    <div class="item" ng-click="showOrNot('item')">
      <p><b>blablabla 1</b>
      </p>
    </div>
    <li class="item" ng-hide="show.item">
      <input type="text">
    </li>

    
    
    <div class="item" ng-click="showOrNot('item2')">
      <p><b>blablabla 2</b>
      </p>
    </div>
    <li class="item" ng-hide="show.item2">
      <input type="text">
    </li>
    
    
    <div class="item" ng-click="showOrNot('item3')">
      <p><b>blablabla 3</b>
      </p>
    </div>
    <li class="item" ng-hide="show.item3">
      <input type="text">
    </li>
    
  </div>

</div>

更多相关文章

  1. 使用HTML文件中的React调用.js文件中的Javascript函数
  2. 如何从控制器内的javascript函数调用角范围函数
  3. 在传递给google.setOnLoadCallback()的函数中使用参数;
  4. 另一个iframe中的iframe的onload函数
  5. JavaScript 中的函数介绍
  6. ABP(现代ASP.NET样板开发框架)系列之21、ABP展现层——Javascrip
  7. 为什么括号用于包装javascript函数调用? [重复]
  8. javascript 构造函数中的属性与原型上属性优先级的比较
  9. 在javascript中调用带有双参数()的函数

随机推荐

  1. FreeMarker 概述
  2. 有了Python,我能叫出所有猫的名字
  3. Android(安卓)Studio查看Android(安卓)5.
  4. 排序算法 #6 快速排序
  5. 为什么要用Dubbo?
  6. Python字符串必备速查表.pdf
  7. 排序算法 #3 插入排序
  8. 给Python学习者的文件读写指南(含基础与进
  9. 每日前端夜话(0x02):ECMAScript 2016,2017
  10. 排序算法 #7 双路快排