AngularJS ng-options not rendering values

前端 未结 4 1622
说谎
说谎 2020-12-17 16:37

How can I render the value of the following options list?

$scope.limits = [ {value:  \'5\', text: \'Afficher 5 par page\'},
                  {value: \'10\',         


        
相关标签:
4条回答
  • 2020-12-17 17:23

    The ng-options directive does not set the value attribute on the <options> elements. It always uses a sequence.

    Using limit.value as limit.text for limit in limits means:

    • Set the <option>'s label as limit.text
    • Save the limit.value value into the select's ng-model

    Check this fiddle: http://jsfiddle.net/bmleite/k58Hw/

    0 讨论(0)
  • 2020-12-17 17:26

    I realize this is an old question but I had an issue with this too. My simple solution to this was to create a hidden input and bind it using ngModel with the select. Then when you do normal form submit, submit the hidden input. Hope this helps.

    0 讨论(0)
  • 2020-12-17 17:28

    for an alternate approach, we can use ng-repeat

    <select  ng-model="limit" id="limitType" class="ng-pristine ng-valid" >
     <option ng-repeat="option in limits " value="{{option.value}}" >
      {{option.text}}
     </option>
    </select>
    
    0 讨论(0)
  • 2020-12-17 17:38

    To make it work, you (we) have to change the way we submit the data to the server.

    The browser is "stupid", and it does not take care about AngularJS, jQuery or any other JavaScript framework doing magic in the code behind. If the option contains index value instead of real value, the browser will send the index value to the server.

    If you want to make it work, you either have to use ng-repeat in conjunction with option tag to render correct values, or to submit data by calling the .post method from the AngularJS controller.

    This is why I love & hate AngularJS.

    0 讨论(0)
提交回复
热议问题