Hi All,
I am using Ionic 2
.
I am getting this error, and I cannot figure out why. I am creating an object with a constructor, but the error states there is no constructor. Any help appreciated.
error
TypeError: LocationModel_1.LocationModel is not a constructor
var locationModel = new LocationModel_1.LocationModel();
code
var locationModel: LocationModel = new LocationModel();
LocationModel.ts
import { Injectable } from "@angular/core";
@Injectable()
export class LocationModel {
public id: number = null;
public latitude: number = null;
public longitude: number = null;
constructor() {
}
}
This post says it is a bug with Typescript:
This is a bug in TS 1.8.10 and fixed in master.
What do I do to fix this? Do I upgrade my Typescript? How do I know what version I am using? How do I upgrade?
npm install -g typescript
installs typescript@1.8.10
, so I guess this is the latest version? What do they mean it’s fixed on master
, how do I install master
? Here also shows that 1.8.10
is the latest version.
npm install typescript@next
has not fixed this error.
I have the same problem. Did you find anything which fixes it?
I found something not very elegant. If I add var locationModel: LocationModel = new LocationModel();
outside the loop (not used), it seems to fix it, not sure why though. See it’s defined outside the loop, and then inside again where it was previously failing. So I guess it’s some scope issue.
saveMarkers(): void {
var locationModel: LocationModel = new LocationModel();
var locationModels: LocationModel[] = [];
for (var index = 0; index < this.markers.length; index++) {
let marker: google.maps.Marker = this.markers[index];
let location: google.maps.LatLng = marker.getPosition();
var locationModel: LocationModel = new LocationModel();
locationModel.latitude = location.lat();
locationModel.longitude = location.lng();
locationModels.push(locationModel);
}
this.employeeModel.locations = locationModels;
}
Hi, thanks for the reply.
I also have it in a loop. But sadly for me, it doesn’t matter if I put it outside the loop or not.
I even have it as a parameter in a different view, getting the same error on it.
export class PictureDetailPage {
picture : Picture = new Picture();
}
If you’re interested, I managed to solve my problem here.
1 Like
Thank you. That was causing my problem too. I had a Capital letter in the import.
Good to hear that it solved your problems as well 
1 Like