[英]AngularJs select ng-options not working
I am trying to make a select in AngularJS without success. 我试图在AngularJS中进行选择但没有成功。
I have two objects: 我有两个对象:
AnimalType 动物类型
$scope.animalTypeArray = {idAnimalType:"xxx", name:"yyy"}; ...
and Animal 和动物
$scope.animal = new animalObj(); //This one is empty.
this.idAnimalType,
this.name,
this.age ...
what I want to do is to show a list of animalsType and put the idAnimalType into the idAnimalType of the Animal obj. 我要做的是显示animalsType的列表,并将idAnimalType放入Animal obj的idAnimalType中。
I am trying this: 我正在尝试:
<select class="form-control select-primary" id="animalType" ng-model="animal.idAnimalType" ng-options="animalType.getName() for animalType in animalTypeArray track by animalType.getIdAnimalType()">
controller: 控制器:
//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);
When I print console.log($scope.animalTypeArray); 当我打印console.log($ scope.animalTypeArray); the object array is shown OK.
对象数组显示为OK。
The select is shown with as many results as the array have, but they only print a blank char, and not the expected name. 显示的选择结果与数组的结果一样多,但它们仅显示一个空白字符,而不显示预期的名称。
What am I doing wrong? 我究竟做错了什么?
Are you looking for something like this? 您是否正在寻找这样的东西? Its a rough version of what you are trying to do ... I have improvised animalTypeObj and animalObj as follows ...
这是您要尝试做的工作的粗略版本。我现将即席的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;
}
I have skipped 我跳过了
http://jsfiddle.net/c2zamajc/5/ http://jsfiddle.net/c2zamajc/5/
Hope it helps .... 希望能帮助到你 ....
Since you are not providing much of the code i assume you need something like this: 由于您没有提供很多代码,因此我假设您需要以下代码:
<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.