我正在使用Angular 1.5.7和Typescript的现有应用程序添加功能,我之前没有使用过这些技术,而且已经遇到了死胡同。
我收到以下错误 TypeError:无法读取未定义的属性'InviteUser'
这是我的代码。
AddEditContactController.ts
import CT = require('../../models/commontypes');
import E = require('../../models/Employers');
import * as Common from "../../../common/commonapp";
import EmployerService = require('../services/EmployerService');
class AddEditContactController {
static controllerId = 'addEditContact';
static $inject = ['$uibModalInstance', '$routeParams', '$http', Common.LookupService.serviceId, 'Item' ];
public InviteSent: boolean = false;
public OrganisationID: number;
constructor(protected $modalInstance: ng.ui.bootstrap.IModalServiceInstance,
private $routeParams: E.Employed.IViewEmployerProfesionalBodyRouteParams,
protected $http: ng.IHttpService,
private LookupService: Common.LookupService,
public Item: E.Employed.Contact,
private EmployerService: EmployerService)
{
this.OrganisationID = +$routeParams.EmployerID;
}
public Cancel() {
this.$modalInstance.dismiss('cancel');
}
public Save(form) {
if (form.$valid) {
//don't need to pass anything back because the same instance we passed in will have been manipulated
this.$modalInstance.close(this.Item);
}
}
public SendContactInvite(form) {
if (form.$valid) {
this.EmployerService.InviteUser(this.Item.ID, this.OrganisationID).then(() => { // The Error is pointing to this line
this.InviteSent = true;
});
}
}
}
export = AddEditContactController;
错误指向SendContactInvite函数中的行,但ItemID和OrganisationID都包含一个值,我错过了什么吗?
这是其余的
EmployerService.ts
InviteUser(ContactID: number, OrganisationID: number): ng.IHttpPromise<{}> {
return this.$http({
url: '/employer/InviteUser',
method: 'POST',
data: {
ContactID: ContactID,
OrganisationID: OrganisationID
}
});
}
AddEditContact.html
<div class="form-group">
<div class="pull-right ng-scope">
<button type="button" class="btn btn-employed" ng-click="modal.SendContactInvite(form)" ng-disabled="form.$invalid"><span class="glyphicon glyphicon-send" aria-hidden="true" uib-tooltip="Invite contact to become a member" tooltip-append-to-body="true"></span> Send Invite</button>
</div>
任何帮助/建议将不胜感激,提前谢谢
答案 0 :(得分:0)
当您指定应该在类上注入的内容时,未指定EmployerService
static $inject = [
'$uibModalInstance',
'$routeParams',
'$http',
Common.LookupService.serviceId,
'Item'
];
您需要将其添加到注入项目列表中:
static $inject = [
'$uibModalInstance',
'$routeParams',
'$http',
Common.LookupService.serviceId,
'Item',
'EmployerService' // <---- NEW!!!
];