2

I have this code

<select ng-model='item.data.choice' >
  <option ng-if='item.data.simple_allow_blank == false' ng-show='choice.name' ng-repeat='choice in item.data.simple_choices' value="{{choice.name}}" ng-disabled="$parent.controllerAction == 'show'">{{choice.name}}</option>
</select>

It shows the 3 choices entered by the Admin of the system in a dropdown box. It shows it without the blank choice.

The problem is if I want to put a default value (for example, choice[0]), it adds a blank value.

Also when converting from ng-repeat to ng-options from this question.

I have 4 choices 1,2,3 and blank i wanted to remove the blank i followed the guide/answers found on the link i have posted with my question, the difference in my part is that i have a dynamic list and when the list is newly created. it never fails to include the blank when using ng-option but it solves my problem to have an initial default value from the dynamic list, if i use ng-repeat i dont have the initial value but the blank from the choices where remove. i wanted to have the blank choice remove and the same time to have an initial value from the dynamic list.

I can set the default value but it cannot remove the blank option.

Can anyone help? Thanks.

mikwat
  • 533
  • 3
  • 11
shimukawa
  • 21
  • 2

2 Answers2

0
You have to filter data in js file and create a new array that you will render it in html.let say you are getting data in variable **item.data.simple_choices**(as you have written it in ng-repeat)then your function would be.

var data = item.data.simple_choices;
var filteredArray = [];
data.filter(function(item){
  if(item.name!=''){
filteredArray.push(item)
}
})
gaurav bankoti
  • 224
  • 1
  • 8
0

After hours of leeching the web and lots of trial and error I finally got the correct answer.

<select ng-model='model'>
        <!-- correct binding -->
          <option ng-show='choice.name'  ng-repeat='choice in object' value="{{choice.name}}" ng-selected='model = object[0].name'>{{choice.name}}</option>
      </select>

The code is simple but this format is helpful because object is an object the has the value of the list. as you can see i did assign it to the model because model is the variable that is being used in the view. The [0] is the index number of the list. You can use value or name depending on what parameters are present on the object.

shimukawa
  • 21
  • 2