1

I'm trying to define a function that can be used to set the default value on a select. The select is populated correctly, but the value passed into the function is undefined. How do I accomplish this?

This is my HTML.

        <div id="Option" class="form-group" ng-show="Options.length > 0">
            <div class="col-sm-1 col-md-1 col-lg-2" />
            <label class="control-label col-sm-3 col-md-3 col-lg-2">Options</label>
            <div class="col-sm-2 col-md-2 col-lg-2" >
                <select name="OptionSelect" id="OptionSelect" class="form-control"
                        ng-model="OptionSelectedId"
                        ng-init="OptionSelectedId = getDefaultOption(Options)" 
                        ng-options="Option.objectKey as Option.label for Option in Options" required >
                </select>
            </div>
        </div>

This is the function on the controller scope

$scope.getDefaultOption=function(arr){
        return arr=$.grep(arr, function (dropdownObj) {
            return dropdownObj.default === true;
        })[0];  

}

1 Answer 1

2

try this:

ng-init="getDefaultOption(Options)"

$scope.getDefaultOption=function(arr){
        arr=$.grep(arr, function (dropdownObj) {
            $scope.OptionSelectedId = dropdownObj.default === true;
        })[0];  

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

1 Comment

just cal function with param in ng-init - your function should set $scope.OptionSelectedId inside js file, not in view like in your example. Hope it helps to solve your problem :)

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.