我试图在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显示的结果与数组一样多,但它们只打印一个空白字符,而不是预期的名称。
我做错了什么?
答案 0 :(得分:0)
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)
由于您没有提供太多代码,我认为您需要这样的代码:
+