0

If I write this:

    <select name="udaljenosti_{$index}}" class="form-control" required>
       <option value="">Choose</option>
       <option ng-repeat="choice in attribute.Choices" value="{{choice.Id}}" ng-selected="choice.IsSelected==true">{{choice.Name}} </option>
     </select>

I don't get validation working. But when I write this:

 <select name="udaljenosti_{{$index}}" ng-model="attribute.Choices.Name" ng-options="item.Id as item.Name  for item in attribute.Choices" class="form-control" required ng-required="true">
       <option value="">Choose</option>
 </select>

validation is working but I don't know how to prepopulate select with selected option (IsSeletected property from the first example) This is JSON response: enter image description here

6
  • Actually am confused. What you want? do you want default selected option? or what? And where you using validation code? Commented Apr 13, 2015 at 11:27
  • @Ramesh Rajendran Read first example. Do you see something like this: ng-selected="choice.IsSelected==true". choice list has IsSelected attribute on it. Commented Apr 13, 2015 at 11:28
  • Yes i saw that, ng-selected helps to set a default value. but you mean by it a validation purpose, That's a point for my confusion Commented Apr 13, 2015 at 11:29
  • Validation is not working in first example althought there is ng-required attribute. In second example validation is working but I don't know how to make third option selected(If we asume third option has IsSelected property we talked about) Commented Apr 13, 2015 at 11:31
  • If the question is not easy to understand I will clearfy. I don't see reason for downvoting. Commented Apr 13, 2015 at 11:32

2 Answers 2

1

Didn't get any error

tried your code, with predefined value in ng-model.

<div ng-app>
  <h2>Todo</h2>
  <div ng-controller="TodoCtrl">
    <select   ng-model="selectedPerson" ng-options="p.first as p.last for p in people" class="form-control" required ng-required="true">
       <option value="">Choose</option>
 </select>
     {{ selectedPerson }}
  </div>
</div>

function TodoCtrl($scope) {

    $scope.people = [
        { id: 1, first: 'John', last: 'Rambo', actor: 'Silvester' },
        { id: 2, first: 'Rocky', last: 'Balboa', actor: 'Silvester' },
        { id: 3, first: 'John', last: 'Kimble', actor: 'Arnold' },
        { id: 4, first: 'Ben', last: 'Richards', actor: 'Arnold' }
    ];
    $scope.selectedPerson = 'Ben'; 

}

here is the working jsfiddle enter link description here

you just need to make sure ,than value is ng-model is right, in your case must match with what your ID AngularJS will automatically show you correct option selected in dropdown.

YOUR ERROR IS YOU ARE USING ng-model="attribute.Choices.Name , instead of ng-model="attribute.Choices.Id

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

8 Comments

I have more complicated situation. My select's are inside ng-repetar. I found solution. name="udaljenosti_{{$index}}" ng-model="udaljenosti_{{$index}}". ng-model and name must match to make validation working.
But there is another problem. i dont' know how to use {{}} interpolaction inside ng-model
wait. In ng-repeat , you don't need ng-model. ng-repat is independent of ng-model.
This helped: YOUR ERROR IS YOU ARE USING ng-model="attribute.Choices.Name , instead of ng-model="attribute.Choices.Id. Thanks
There is also one problem. When the selected is untouched and prepopulated from database I get error that is required when I submit the form.
|
0

Try this code ng-selected="attribute.choice[$index].IsSelected==true"

try to use this instead of your code.

 <select name="udaljenosti_{{$index}}" ng-model="attribute.Choices.Name" 
 ng-options="item.Id as item.Name  for item in attribute.Choices"
 class="form-  control" required ng-required="true" 
 ng-selected=**"attribute.choice[$index].IsSelected==true"**>
                 <option value="">Choose</option>
                </select>

1 Comment

No, it is not working, I made edit on question to make it more understandable (I deleted "$index").(I have select elements inside ng-repeater, that's why I had $index in name attribute)

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.