【问题标题】:Select tag ng-model returns an undefined value选择标签 ng-model 返回一个未定义的值
【发布时间】:2018-03-05 22:53:08
【问题描述】:

我的 html 代码中有一个 select 标记,如下所示:

<select ng-model="brandList" name="brandList"  style="width:110px;" >
    <option value="" selected>---Please select---</option>
    <option ng-repeat="item in brandnameList | unique:'brandname'" value="{{item.brandname}}" style="width:50px;"> {{item.brandname}}</option>

</select>

我的选择中的值是通过 API 从数据库中获取的,代码如下所示。

adminService.getbrandnameList()
                .then(function(data){
                    $scope.brandnameList = data.data;
                    $scope.brandn=data.data[0].brandname;

            }); 

我有另一个函数需要选择标签上的选定值

    $scope.ExportmodalAdmin = function () {
            alert(  $scope.brandList )
    }

但是 $scope.brandList 的选择模型返回一个未定义的值。我怎样才能解决这个问题?我需要将 select 标记中的值传递给函数。

【问题讨论】:

  • 发布您从 adminService.getbrandnameList() 获得的 JSON 示例
  • 您的代码是正确的,但您缺少定义变量 $scope.brandList = {};在方法 ExportmodalAdmin 之前
  • 你能把你的代码贴出来吗
  • @Chandru,我在方法 exportmodalAdmin 之前定义了变量 $scope.brandList ={} 但它返回 {Object object} 响应

标签: javascript html angularjs select angular-ngmodel


【解决方案1】:

希望您在控制器中定义了变量brandList。

$scope.brandList = {};

另外,将您的 ng-repeat 语句更改如下:

 <select ng-model="brandList" name="brandList" ng-options="item.brandname for item in brandnameList" style="width:110px;" ng-change="alertChange()" >
    <option value="" selected>---Please select---</option>
</select>

检查更改的控制器代码:

$scope.alertChange = function(){
    alert($scope.brandList.brandname);
  };

在这里查看 plunker 参考 plunk

【讨论】:

  • 我在控制器中定义了变量brandList,但它返回{object |object}值。当我更改我的 ng-repeat 语句时,下拉选择中没有列出任何值
  • 你能把getbrandnameList()函数的结果贴出来
  • getbrandnameList() 函数的结果是这样的 [ { "brandname": "Aromatherapy Associates" }, { "brandname": "ST.Tropez" }, { "brandname": "Sampar " }, { "brandname": "Jack Black" }, { "brandname": "CeraVe" } ]
  • 我已经更新了 ng-repeat 部分。测试一下,让我知道
  • 下拉列表中仍然没有显示值
猜你喜欢
  • 2018-11-13
  • 2016-12-30
  • 1970-01-01
  • 1970-01-01
  • 2017-04-29
  • 2015-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多