我遇到了一个函数在select中返回未定义的问题。
<select name="supervisor"
ng-model="editJudge.superID"
ng-options="supervisor.legalProID as supervisor.fullName for supervisor in supervisors"
class="form-control">
这是我加载主管的功能。
function getLegalProfessionalsBroken() {
LegalProfessionalResource.query({isValid: true}).$promise.then(function(legalProfessionals) {
$scope.supervisors = _.map(legalProfessionals, function(legalProfessional) {
return new LegalProfessional(legalProfessional);
});
});
}
这是我的LegalProfessional课程。
function LegalProfessional(obj) {
var properties = $.extend({
legalProID: null,
firstName: null,
lastName: null,
middleName: null,
email: null,
isActive: true,
insertedDate: null,
insertedBy: null,
modifiedDate: null,
modifiedBy: null,
deletedDate: null,
deletedBy: null,
typeID: null,
superID: null
}, obj);
this.legalProID = properties.legalProID;
this.firstName = properties.firstName;
this.lastName = properties.lastName;
this.middleName = properties.middleName;
this.email = properties.email;
this.isActive = properties.isActive;
this.insertedDate = properties.insertedDate;
this.insertedBy = properties.insertedBy;
this.modifiedDate = properties.modifiedDate;
this.modifiedBy = properties.modifiedBy;
this.deletedDate = properties.deletedDate;
this.deletedBy = properties.deletedBy;
this.typeID = properties.typeID;
this.superID = properties.superID;
}
LegalProfessional.prototype = {
fullName: function() {
return this.lastName + ', ' + this.firstName;
}
};
当我像这样运行时,我得到的是未定义的,未定义的&#39;对于所有选项。如果我在其中设置一个断点,那么这就是&#39;范围限定为html元素而不是LegalProfessional。
我可以在LegalProfessional类中注释掉fullName函数,然后更改我的getLegalProfessionals类以添加fullName,并且事情按预期工作。
function getLegalProfessionals() {
LegalProfessionalResource.query({isValid: true}).$promise.then(function(legalProfessionals) {
$scope.supervisors = _.map(legalProfessionals, function(legalProfessional) {
return new LegalProfessional(legalProfessional);
});
$scope.supervisors.forEach(function(supervisor) {
supervisor.fullName = function() {
return supervisor.lastName + ', ' + supervisor.firstName;
};
});
});
}
我可以在控制器和$ scope.supervisors [0] .fullName()中设置断点的两种方式都可以获得我希望看到的值。
我宁愿选择第一个选项,所以我只有一个地方可以维护它,因为我在我的应用程序中的几个地方使用它。
我确定这是ng-repeat的范围问题,但我不确定如何对抗它。