简体   繁体   English

AngularJS嵌套ng-repeat单击并显示/隐藏

[英]AngularJS nested ng-repeat click & show/hide

I've done a ton of reading and research on this topic the past few days and have found some good answers, but for some of the answers I question performance and necessity. 过去几天,我已经对该主题进行了大量阅读和研究,并找到了一些不错的答案,但是对于某些答案,我质疑性能和必要性。

My question pertains to nested ng-repeat scopes. 我的问题与嵌套ng-repeat范围有关。 I'm wondering what the best way to achieve an "add item" scenario for adding an item to the nested foreach. 我想知道实现将项目添加到嵌套的foreach的“添加项目”方案的最佳方法是什么。

My Code 我的密码

My HTML is simply 2 ng-repeat s and my goal is to be able to add an item to the second (nested) ng-repeat 我的HTML只是2 ng-repeat ,我的目标是能够向第二个(嵌套的) ng-repeat添加项目

<div ng-app="myApp">
    <div class="nav" ng-controller="FoodsController as vm">
        <div class="level1" ng-repeat="foods in vm.foodGroups">{{foods.Name}}
            <button type="button" ng-click="vm.addNewFood()">add new food</button>
            <div ng-show="vm.newFoodBeingAdded">
                <input type="text">
            </div>
            <div class="level2" ng-repeat="food in foods.FoodsInGroup">{{food.Name}}</div>
        </div>
    </div>
</div>

My Angular controller looks like this: 我的Angular控制器如下所示:

app.controller('FoodsController', function () {
    var vm = this;
    vm.foodGroups = [{
        "Name": "Grains",
            "FoodsInGroup": [{
            "Name": "Wheat"
        }, {
            "Name": "Oats"
        }]
    }, {
        "Name": "Fruits",
            "FoodsInGroup": [{
            "Name": "Apple"
        }, {
            "Name": "Orange"
        }]
    }];

    vm.newFoodBeingAdded = false;

    vm.addNewFood = function () {
        vm.newFoodBeingAdded = true;
    };
});

What should happen 应该发生什么

The general work flow would be a user clicks an Add New button and it shows a text box with a "save" button. 常规工作流程是用户单击“ 添加新”按钮,它显示带有“保存”按钮的文本框。 The text box & button would be within the parent foreach. 文本框和按钮将位于父foreach中。 Once a user saves the item it would then be added to the nested foreach (that logic isn't shown). 一旦用户保存了该项目,它将被添加到嵌套的foreach中(该逻辑未显示)。

The issue 问题

The issue is that when I click "Add New Food" (which should just show 1 of the text boxes & save buttons), all of the text boxes show. 问题是,当我单击“添加新食物”(应该只显示其中一个文本框和保存按钮)时,所有文本框都会显示。 How do I ensure I am "scoping" this correctly and that only the text box/button within that parent are shown? 如何确保正确“确定范围”,并且仅显示该父级中的文本框/按钮?

Possible solution 可能的解决方案

One answer I found was to create a child controller for each nested item. 我发现的一个答案是为每个嵌套项目创建一个子控制器。 For example I'd have a FoodGroupsController which would manage all the logic for the nested foreach (because there will be a lot more going on than just adding a new item in a real app, so it could be justified). 例如,我有一个FoodGroupsController ,它将管理嵌套的foreach的所有逻辑(因为除了在真实的应用程序中添加新项之外,还有很多事情要做,因此可以证明是合理的)。

jsFiddle jsFiddle

Here's a jsFiddle with the code that currently does not function correctly. 这是一个jsFiddle ,其中包含当前无法正常运行的代码。

There is the forked Fiddle 有分叉的小提琴

I made just few changes. 我做了几处更改。 The fact is that you were binding the ng-show with a single var in your controller. 事实是您将ng-show与控制器中的单个var绑定在一起。 It was a show me all or show me nothing possibility. 这是向我展示全部或向我展示任何可能性。 So the fix it, you have to bind this, in your food item, not in the controller himself. 因此,要解决此问题,您必须将此绑定在食物中,而不是在控制器本身中。

Html : HTML:

<button type="button" ng-click="vm.addNewFood(foods)">add new food</button>
    <div ng-show="foods.newFoodBeingAdded" class="add-new-food">
       <input type="text" placeholer="add a new food">
       <button type="button">save new food</button>
</div>

Controller : 控制器:

vm.addNewFood = function (foods) {
    foods.newFoodBeingAdded = true;
};

With this code, you pass the food in param of your function, so you can change the boolean of your food only. 使用此代码,您可以在函数的参数中传递食物,因此您只能更改食物的布尔值。 And then, your ng-show is just binding on this boolean. 然后,您的ng-show只是绑定到此布尔值上。

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

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