javascript - 在 ng-repeat angularJS 中仅隐藏和显示每个对象的输入字段

标签 javascript angularjs angularjs-ng-repeat angularjs-ng-show

所以我有一组位置和描述的输入字段,我正在重复第二组的显示和隐藏按钮

所以我的html:

<div ng-repeat="location in vm.locations" class="row">
    <div class="form-group col-md-12">
        <label for="location">Location</label>
        <input type="text" class="form-control" name="location" ng-model="location.name">

        <a class="pull-right" ng-click="vm.showLocationDetail()">Add Description</a>
    </div>
    <br>
    <div ng-show="vm.showDetail" class="form-group col-md-12">
        <textarea class="form-control" type="text" name="description" rows="5" ng-model="location.description"></textarea>
    </div>
</div>

Controller :

// toggle location details
        vm.showLocationDetail = function() {
            return vm.showDetail = !vm.showDetail;
        }

现在,如果我有超过 1 个位置字段并且我单击“添加描述”...每个字段都会显示描述输入字段。

如何使其仅显示相关的描述字段?

最佳答案

这是因为您使用单个变量来显示和隐藏所有描述字段。使用位置的某些属性来显示或隐藏它。

var MyApp = angular.module("MyApp",[]);
MyApp.controller("MyCtrl",['$scope',MyCtrl]);
function MyCtrl($scope) {
$scope.locations = [{name:'',description:'',showDetail : false},{name:'',description:'',showDetail : false}];
$scope.showLocationDetail = function(location) {
   location.showDetail = ! location.showDetail;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div ng-app="MyApp" ng-controller="MyCtrl">
<div ng-repeat="location in locations" class="row">
    <div class="form-group col-md-12">
        <label for="location">Location</label>
        <input type="text" class="form-control" name="location" ng-model="location.name">

        <a class="pull-right" ng-click="showLocationDetail(location)">Add Description</a>
    </div>
    <br>
    <div ng-show="location.showDetail" class="form-group col-md-12">
        <textarea class="form-control" type="text" name="description" rows="5" ng-model="location.description"></textarea>
    </div>
</div>
</div>

关于javascript - 在 ng-repeat angularJS 中仅隐藏和显示每个对象的输入字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43929736/

相关文章:

javascript - Angular 样板和 Twitter Bootstrap 3

javascript - 自定义指令上的 Angular 属性困惑

javascript - Angular 2 应用程序中的子路由

javascript - ng-repeat 更新实时数据时闪烁

c# - 需要显示附加文件的文件名,文件上传到使用 window.open 打开的单独页面上

javascript - 如何使用 Angular 在 rxJs 订阅和映射中链接 API 调用?

php - java查询链接错误

javascript - 将 ng-repeat 过滤器迁移到 Typescript

javascript - ng-repeat 运行两次(Angular)

javascript - 将自定义实例方法动态添加到 Mongoose 模式