سؤال

I'm working on a form generator implementation, and I want to have ng-options evaluate a property of the current 'field' and return the select 'options' as listed in something like $scope.options.

This means that I can setup the 'option' containing objects in my controller, and have the form elements use the correct object to populate the dropdown.

My form is built from a relational database, so hard coding the target object isn't going to help (although it does work, it's just not scalable)

I'm stuck figuring out how to get ng-options to evaluate an expression which makes the jump from variable to object name.

Maybe there's just a better way to do this altogether?

<select id="{{field.Name}}" ng-model="field.Value"ng-options="option.ID as option.Value for option in [SOMETHING THAT EVALUATES TO AN OBJECT NAMED IN THE BINDING PROPERTY]" ng-required="field.Required"></select>

I have a fiddle here: JSFiddle

هل كانت مفيدة؟

المحلول

I would make an object containing objects of your options

$scope.optiongroups = {group1: [...], group2: [...]}

and then

ng-options="value as option for option in optiongroups[dynamic var here]"
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top