Skip to content Skip to sidebar Skip to footer

Setting Angular Select Ng-init To First Value In Array

I have a simple select element that I am trying to enforce a value being present to submit the form and I have tried both setting the required attribute as well as using ng-init to

Solution 1:

required will only work in side a form element.

What you want is ng-init="model.refmarker=refmarkers[0]"

<!doctype html><htmllang="en"data-ng-app="app"><head><metacharset="utf-8"><title>Test</title><scriptsrc="//ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.js"></script><scripttype="text/javascript">

        angular.module('app',[])
                .controller('Main',function($scope)
                {
                    $scope.model = {};
                    $scope.refmarkers = [{ref:'abc'},{ref:'def'},{ref:'ghi'}];
                });
    </script></head><bodyng-controller="Main">
{{'Angular'}}
<selectng-model="model.refmarker"ng-options="rm.ref for rm in refmarkers"ng-init="model.refmarker=refmarkers[0]"></select>
{{model.refmarker}}
</body></html>

Post a Comment for "Setting Angular Select Ng-init To First Value In Array"