繁体   English   中英

AngularJs选择ng-options不起作用

[英]AngularJs select ng-options not working

我试图在AngularJS中进行选择但没有成功。

我有两个对象:

动物类型

 $scope.animalTypeArray = {idAnimalType:"xxx", name:"yyy"}; ...

动物

$scope.animal = new animalObj(); //This one is empty.

  this.idAnimalType,

  this.name,

  this.age ...

我要做的是显示animalsType的列表,并将idAnimalType放入Animal obj的idAnimalType中。

我正在尝试:

<select  class="form-control select-primary" id="animalType" ng-model="animal.idAnimalType" ng-options="animalType.getName() for animalType in animalTypeArray track by animalType.getIdAnimalType()">

控制器:

//objectsdeclaration
$scope.animalType = new animalTypeObj();
$scope.animal = new animalObj();

$scope.animalTypeArray = new Array();
$scope.animalType;

//objects creation of animalType to show on the select
var animalType = new animalTypeObj();
animalType.construct(1,"Bird");
$scope.animalTypeArray.push(animalType);

var animalType = new animalTypeObj();
animalType.construct(2,"Insect");
$scope.animalTypeArray.push(animalType);

当我打印console.log($ scope.animalTypeArray); 对象数组显示为OK。

显示的选择结果与数组的结果一样多,但它们仅显示一个空白字符,而不显示预期的名称。

我究竟做错了什么?

您是否正在寻找这样的东西? 这是您要尝试做的工作的粗略版本。我现将即席的animalTypeObj和animalObj如下...

  function animalTypeObj(){
     var that = this;

     that.construct = function(type, name){
     that.type = type;
     that.name = name;
     }

     that.getName = function(){return that.name;}
     that.getIdAnimalType = function(){return that.type; }
}

function animalObj(){
     var that = this;
     that.idAnimalType = {};
     that.name = '';
     that.age = 0;
}

我跳过了

http://jsfiddle.net/c2zamajc/5/

希望能帮助到你 ....

由于您没有提供很多代码,因此我假设您需要以下代码:

    <select  class="form-control select-primary" id="animalType" ng-model="animal.idAnimalType"
      ng-options="animalType.idAnimalType as animalType.name for animalType in animalTypeArray">
   </select>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM