1

Howdie do,

I'm attempting to only display an option if the code that the client used to login, matches the $scope.code in the controller.

The HTML should then display the option that matches the code the client logged in with.

View:

<div class="">
<select id="customer-dd" ng-model="selectedCustomer" ng-repeat="(group, msg) in codes">
    <option value="">select...</option>
            <div ng-if=" 'group' == 'code' ">
                 <option value="{{ group }} ">{{ msg }}</option>
             </div>
</select>
</div>

Controller:

$scope.code = dataFactory.getCode();
$scope.codes = {

 'ABC': 'First option',
 'DEF': 'Second option'
}

There should only be one option showing as a client can't login with more than one code at a time

However, when I run this, I keep getting two input boxes instead of just the one that matches the code.

Is there something I'm missing here?

* UPDATE *

I've updated the code to the following and multiple options are still being printed:

<div class="">
<select id="customer-dd" ng-model="selectedCustomer" ng-repeat="(group, msg) in codes">
    <option value="">select...</option>
        <div ng-if=" group == code ">
            <option value="{{ group }} ">{{ msg }}</option>
        </div>
</select>
</div>

* UPDATE * @ieaglle Removing the div allowed the if statement to excecute. The updated HTML is now:

<div class="">
<select id="customer-dd" ng-model="selectedCustomer" ng-repeat="(group, msg) in codes">
    <option value="">select...</option>
    <option ng-if=" group == code " value="{{ group }} ">{{ msg }}</option>
</select>
</div>

THANKKKK UUUU!!!

9
  • What version of angular? Commented Sep 11, 2015 at 16:23
  • you are doing it wrong , remove single quote from group Commented Sep 11, 2015 at 16:24
  • Thank you for your reply, however even when I remove the single quotes, I'm still getting every option printed instead of just the one Commented Sep 11, 2015 at 16:33
  • 1
    Is it okay to wrap option in div? I've never seen that. Why not apply ng-if on option itself? Commented Sep 11, 2015 at 16:41
  • 1
    @Jimmy, please see this plnkr.co/edit/CzguhLmnbCabvBYnRwhc?p=preview Commented Sep 11, 2015 at 16:56

2 Answers 2

1

Try using ng-options instead with a filtered object.

http://jsfiddle.net/joshdmiller/hb7lu/

HTML:

<select ng-model="selectedCustomer" ng-options="msg for (group, msg) in filterObjsByProp(codes)"></select>

JS:

$scope.code = 'ABC';
$scope.codes = {

    'ABC': 'First option',
        'DEF': 'Second option'
};

$scope.filterObjsByProp = function (items) {
    var result = {};
    angular.forEach(items, function (value, key) {
        if (key === $scope.code) {
            result[key] = value;
        }
    });
    return result;
}

Although this is overkill, since an object cannot have multiple properties with the same name, so you will only ever have 1 option in the select. As such, maybe a select is not the best option here, or maybe an array with key/value objects is better.

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

1 Comment

Thank you for your reply, however even when I remove the single quotes, I'm still getting every option printed instead of just the one
0

Change your HTML to this. Notice the change in the ng-if statement.

<div class="">
<select id="customer-dd" ng-model="selectedCustomer">
    <option value="{{ group }}" ng-repeat="(group, msg) in codes">select...
            <div ng-if=" group == code ">
                {{ msg }}
             </div>
    </option>
</select>
</div>

3 Comments

Thank you for your reply, however even when I remove the single quotes, I'm still getting every option printed instead of just the one
I made some edits. First I moved the loop to the option level and removed the nested option. Maybe this is what you are looking for.
Still, the same. It's printing both options instead of just the one that matches the code

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.