angularjs-添加多个硬编码选项以选择带有ng选项的框

angularjs-添加多个硬编码选项以选择带有ng选项的框,angularjs,ng-options,Angularjs,Ng Options,我有一个选择框,其中填充了ng选项。我知道您可以手动添加默认选项,如下所示: <select ng-model="selectedAddress" ng-init="selectedAddress = selectedAddress || address_dropdown[0].value" ng-change="handleStoredAddressClick2()" ng-options="a.dropdown_display for a in address_dropdown" i

我有一个选择框,其中填充了ng选项。我知道您可以手动添加默认选项,如下所示:

<select ng-model="selectedAddress" ng-init="selectedAddress = selectedAddress || address_dropdown[0].value" ng-change="handleStoredAddressClick2()" ng-options="a.dropdown_display for a in address_dropdown"  id="address_dropdown">
    <option value="" ng-selected="selected">Add a new address</option>
</select> 
在angular中有没有办法添加多个选项?客户机现在希望有第一个默认选项,比如从地址中选择,并将上面显示的添加新地址选项作为列表中的最后一个选项,位于ng选项中所有填充的选项之后

我知道我可以用jQuery来添加这个选项,但如果可能的话,我更愿意保持它的角度。

编辑2:给你,下层选民:你可能会注意到,这篇文章是从2014年3月开始的。使用来源,卢克!我不再喜欢angularJs了


编辑:如果你想像艾美提到的那样将模型绑定到一个非字符串上,这就行不通了

试试这个:

    <select ng-model="YourModel">
      <option value="" ng-selected="selected">an option</option>
      <option ng-repeat="item in items">{{item.name}}</option>
      <option value="">another option</option>
    </select> 

祝你周末愉快

以下是一个示例,手动添加选项,但不包含ngOptions,但仍绑定到我认为与您的问题相关的模型:

注意:我把空的“-请选择”和“添加新的单元”选项放在中间,看看它们是如何被选择的。 默认情况下,当登陆页面ng selected将“Add new unit”(添加新单位)设置为true时,我想知道angular smartly detects“-Please Choose-”是否也可以被选中,因为它的型号为空,并且只选择了最后一个true选项,因为知道在常规下拉列表中不能有两个选中的选项

如果设置$scope.currentquestion.someOtherParam=false;然后运行,您将得到-请选择-作为默认选择

然后设置$scope.currentquestion.metric_pref='stn';然后运行,则应将石头设置为默认值

然后更改另一个选项,可以看到模型正在更新

<div ng-app="myApp">
    <div ng-controller="MyCtrl">

        <h3>{{currentquestion.text}}</h3>

        <select 
          ng-model="currentquestion.metric_pref"
          name="{{currentquestion.qid}}"
          id="{{currentquestion.qid}}"       
         >

         <option value="kg">
              kg.
         </option>
         <option value="">
             --Please choose--
         </option>
         <option value="lbs">
              pounds
         </option>
        <option value="add" ng-selected='currentquestion.someOtherParam'>
             Add new unit
         </option>
         <option value="stn">
              stones            
         </option>

        </select>
        CurrentVal:{{currentquestion.metric_pref}}
    </div>
</div>
var myApp = angular.module('myApp', []);

myApp.controller("MyCtrl", function ($scope) {
    $scope.currentquestion = {};
    $scope.currentquestion.qid = "QS1";
    $scope.currentquestion.text = "What unit do you prefer to state your weight in?";    
    $scope.currentquestion.metric_pref = '';//can put stn in here to default at stones
    $scope.currentquestion.someOtherParam = false;
    //$scope.currentquestion.unit_options = [ 'lbs' , 'stones' , 'kg' ]; //lbs. for US, stones for UK, metric for others
    //not using ng-options to build the value/text of the option as I need to use ng-translate in the code

});

修改控制器以提供地址下拉列表和其他选项

<select ng-model="selectedAddress" ng-init="selectedAddress = selectedAddress || address_dropdown[0].value" ng-change="handleStoredAddressClick2()" ng-options="a.dropdown_display for a in getAddress_dropdown(address_dropdown)"  id="address_dropdown">
  <option value="" ng-selected="selected">Add a new address</option>
</select> 

$scope.getAddress_dropdown=function(data)
{
    var temp=[{dropdown_display: 'Select from addresses'}];
    return temp.concat(data);
}

您必须在$scope.address\u dropdown中填充这些默认选项如果您希望将模型绑定到非字符串,在我的情况下,它是一个对象,您需要使用ng选项,而不是ng repeat。看到上面的便条了吗?哎呀,我不知道。谢谢我将编辑我的回答我还添加了一个值={{item.Value}}-我找不到使用ng选项添加硬编码的最后一个选项的方法,我需要将所有显示为最后一个选项。。。