AngularJs选择ng-options不起作用

时间:2016-02-27 18:07:06

标签: javascript angularjs angularjs-ng-options

我试图在AngularJS中做出选择而没有成功。

我有两个对象:

AnimalType

 $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。

select显示的结果与数组一样多,但它们只打印一个空白字符,而不是预期的名称。

我做错了什么?

2 个答案:

答案 0 :(得分:0)

你正在寻找这样的东西吗?它是你想要做的粗略版本...我有即兴创作的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/

希望它有所帮助......

答案 1 :(得分:0)

由于您没有提供太多代码,我认为您需要这样的代码:

+