简体   繁体   English

如果数组中已经存在特定的ng-option,则隐藏它

[英]hide a specific ng-option if it's id already exists in array

I have a drop down that adds items on a page that are binded to a $scope. 我有一个下拉菜单,用于在页面上添加绑定到$ scope的项目。 I basically want to make sure I don't add doubles to the array so my idea is to ng-hide the option if it already exists in the array. 我基本上想确保不向数组中添加双精度字,所以我的想法是对隐藏在数组中的选项进行ng-hide隐藏。 I don't know if it's possible to do something like this because the ng-options is inside the select so I dont know if i can append an ng-hide to them in the first place. 我不知道是否可以做这样的事情,因为ng-options在select里面,所以我不知道我是否可以将ng-hide附加到它们的开头。

Regardless, if it is possible, I would like to hide the option if it's id already exists in a scope. 无论如何,如果范围内已经存在该选项,我想隐藏该选项。

I was thinking you would just loop through each and check, here is my attempt 我本以为你会遍历每个并检查一下,这是我的尝试

$scope.checkifExist = function () {
    var ifExist = false;
    angular.forEach($scope.promptsPlease, function (data) {

        if (data.id == $scope.promptsPlease.id){

            ifExist = true;
        }

    });
    return ifExist;

    };

and so the select in the controller looks like 所以控制器中的选择看起来像

  <select ng-model="fadingSelected" ng-options="type.name for type in fadingTypes track by type.id" ng-hide="checkIfExist()">
            </select>

I don't know if this is even possible, and I think I might just be approaching this all wrong. 我不知道这是否有可能,而且我想我可能正将这一切都弄错了。 The idea is just so I cannot add the same one twice to the array that's binded to the scope. 这样做的想法是,这样我就不能将相同的对象两次添加到绑定到作用域的数组中。 It doesn't necessarily have to ng-hide, it would just be nice. 不一定要隐藏,这会很好。 Any help would be appreciated! 任何帮助,将不胜感激! Thanks for reading. 谢谢阅读。

This previous answer might apply to your problem ( https://stackoverflow.com/a/19329910/1036025 ). 先前的答案可能适用于您的问题( https://stackoverflow.com/a/19329910/1036025 )。

Here is the included demo (plunker): http://plnkr.co/edit/Sf3el0FyUptWq28XqZnI?p=preview 这是包含的演示(插件): http ://plnkr.co/edit/Sf3el0FyUptWq28XqZnI?p=preview

It illustrates 3 ways of achieving this. 它说明了实现此目的的3种方法。 One is using the library lodash to manipulate your data and create a filtered list of the available choices instead of using angular.each . 一种是使用库lodash来操纵数据并创建可用选项的过滤列表,而不是使用angular.each

I would recommend you to have a look at the 3rd explained option in the description (alternative using a filter). 我建议您查看说明中的第3个说明的选项(使用过滤器的替代方法)。

I think it's possible only when ng-repeat option tag 我认为只有在ng-repeat选项标签下才有可能

<select ng-model="fadingSelected">
    <option ng-if="checkIfExist()" ng-bind="type.name" ng-value="type.id" ng-repeat="type in fadingTypes track by type.id"></option>
</select>

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

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