0

I have a drop down that adds items on a page that are binded to a $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. 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.

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.

1

2 Answers 2

1

This previous answer might apply to your problem (https://stackoverflow.com/a/19329910/1036025).

Here is the included demo (plunker): http://plnkr.co/edit/Sf3el0FyUptWq28XqZnI?p=preview

It illustrates 3 ways of achieving this. One is using the library lodash to manipulate your data and create a filtered list of the available choices instead of using angular.each.

I would recommend you to have a look at the 3rd explained option in the description (alternative using a filter).

Sign up to request clarification or add additional context in comments.

2 Comments

this is a great example, I am struggling though because it is a single drop down and its tyring to track a sub array thats nested 3 nested ng repeats down.
You should put all the concerned code in your question.
0

I think it's possible only when ng-repeat option tag

<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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.