Angularjs选择值未定义

Angularjs选择值未定义,angularjs,select,ng-options,Angularjs,Select,Ng Options,现在我试图从select下拉列表中获取值,但它返回undefined。在html级别,它的工作方式与预期一样,以下是我的代码: <div class='subcontent'> <input id="me" type="radio" class='choosemethod' ng-model="paymentmethod" value="Y"><label for="me" class='choosemethod'>Me</l

现在我试图从select下拉列表中获取值,但它返回undefined。在html级别,它的工作方式与预期一样,以下是我的代码:

<div class='subcontent'>            
  <input id="me" type="radio" class='choosemethod' ng-model="paymentmethod" value="Y"><label for="me" class='choosemethod'>Me</label>
  <input id="company" type="radio"  ng-model="paymentmethod" value="N" class='choosemethod'><label for="company" class='choosemethod'>My Company</label><br/>
  <span class='helptext'>Who makes payments to this account?</span><span class='help' style='margin-left:20px;width:20px;height:20px;'>help</span>
</div>

<div class='paymentmethods subcontent' ng-switch on="paymentmethod">
  <select ng-model='selectedmethod' ng-init="selectedmethod=Memethods[0]" id='methods' ng-switch-when='Y'>
    <option ng-repeat="method in Memethods" value="{{method}}">{{method}}</option>
  </select>

  <select ng-model='selectedmethod' ng-init="selectedmethod=companies[0]" ng-switch-when='N' style='float:left'>
    <option ng-repeat='companyoption in companies' value="{{companyoption}}">{{companyoption}}</option>
  </select>

  <div class='clear'></div>
  <label for ='methods'>Payment Method</label><span class='help' style='margin-left:20px;width:20px;height:20px;'>help</span>
</div>

它在页面中显示良好,但当我尝试获取值时,它显示未定义。有什么想法吗?

你可以参考这个简单的

html代码:

<div ng-controller="Main" ng-app>
    <div>selections = {{selections}}</div>

    <div>
      <p>Model doesn't get updated when selecting:</p>
      <select ng-repeat="selection in selections" ng-model="selection" ng-options="i.id as i.name for i in items">
        <option value=""></option>
      </select>
    </div>
function Main($scope) {

    $scope.selections = ["", "id-2", ""];

    $scope.reset = function() {
        $scope.selections = ["", "", ""];
    };

    $scope.sample = function() {
        $scope.selections = [ "id-1", "id-2", "id-3" ];
    }

    $scope.items = [{
        id: 'id-1',
        name: 'Name 1'},
    {
        id: 'id-2',
        name: 'Name 2'},
    {
        id: 'id-3',
        name: 'Name 3'}];
}
这是经典的“角点符号”问题

基本上,ng开关会创建一个新的作用域,因此,当select设置selectedmethod属性时,它会在新的作用域上进行设置,而不是您所期望的控制器作用域。一种解决方案是为模型创建父对象

angular.module('app',[])
.controller('main', function($scope){
  $scope.selection = {};
  $scope.Memethods = ['Same as Card/Account Name','American Express','American Express Corp','Cash','Checking','MasterCard','My Visa','VISA'];
  $scope.companies = ['Company Paid','MyAMEX'];
})
请注意它在html中的引用方式不同:

<select ng-model='selection.selectedmethod' ng-init="selection.selectedmethod=companies[0]" ng-switch-when='N' style='float:left'>
  <option ng-repeat='companyoption in companies' value="{{companyoption}}">{{companyoption}}</option>
</select>

{{companyoption}
另一种不同(也许更好)的方法是使用“ControllerAs”语法,它可以为您实现这一点

angular.module('app',[])
    .controller('main', function($scope){

      this.Memethods = ['Same as Card/Account Name','American Express','American Express Corp','Cash','Checking','MasterCard','My Visa','VISA'];
      this.companies = ['Company Paid','MyAMEX'];
    })

<body ng-app="app" ng-controller="main as main">
  <div class='subcontent'>
    <input id="me" type="radio" class='choosemethod' ng-model="paymentmethod" value="Y">
    <label for="me" class='choosemethod'>Me</label>
    <input id="company" type="radio" ng-model="paymentmethod" value="N" class='choosemethod'>
    <label for="company" class='choosemethod'>My Company</label>
    <br/>
    <span class='helptext'>Who makes payments to this account?</span><span class='help' style='margin-left:20px;width:20px;height:20px;'>help</span>
  </div>

  <div class='paymentmethods subcontent' ng-switch on="paymentmethod">
    <select ng-model='main.selectedmethod' ng-init="main.selectedmethod=main.Memethods[0]" id='methods' ng-switch-when='Y'>
      <option ng-repeat="method in main.Memethods" value="{{method}}">{{method}}</option>
    </select>

    <select ng-model='main.selectedmethod' ng-init="main.selectedmethod=main.companies[0]" ng-switch-when='N' style='float:left'>
      <option ng-repeat='companyoption in main.companies' value="{{companyoption}}">{{companyoption}}</option>
    </select>

    <div class='clear'></div>
    <label for='methods'>Payment Method</label><span class='help' style='margin-left:20px;width:20px;height:20px;'>help</span>

  </div>
  <div>{{main.selectedmethod}}</div>
</body>
angular.module('app',[])
.controller('main',函数($scope){
Memethods=[“与卡/账户名相同”,“美国运通”,“美国运通公司”,“现金”,“支票”,“万事达卡”,“我的签证”,“签证];
this.companys=[“公司付款”,“美国运通”];
})
我
我的公司

谁向这个帐户付款?帮助 {{method}} {{companyoption} 付款方式帮助 {{main.selectedmethod}}
ng model inside值在表示之前必须进行初始化。因此,ng init先于ng模型。在选项中,$first用于选择第一个值

<select ng-init= "selectedmethod=companies[0]" ng-model='selectedmethod' ng-switch-when='N' style='float:left'>
    <option ng-repeat='companyoption in companies' value="{{companyoption}}" ng-selected="$first">{{companyoption}} 
    </option>
</select>

{{companyoption}

您是否考虑过使用ng options指令设置select选项?这样,您就不必自己生成选项标签,而且它还有其他优点。一旦我尝试过,我发现它比我想象的要容易。文档可以比我解释得更好:这就是我需要的,我认为这可能是范围问题,但不知道在哪里以及为什么。谢谢你,伙计!解释得很好,尽管我相信您的“controller as”语法的代码示例不是100%正确-您不需要将
Memethods
companys
的其余引用更改为
main.Memethods
/
main.companys
?任何属于控制器实例属性的内容都只能通过scope属性在scope上使用,在本例中,scope属性是“main”。这就是我在ipad上组合答案得到的结果。编辑。谢谢
<select ng-init= "selectedmethod=companies[0]" ng-model='selectedmethod' ng-switch-when='N' style='float:left'>
    <option ng-repeat='companyoption in companies' value="{{companyoption}}" ng-selected="$first">{{companyoption}} 
    </option>
</select>