【发布时间】:2014-09-18 16:51:07
【问题描述】:
学了几天typescript和angularjs,现在有个问题困扰了我好几天,想做一个gps追踪系统,所以尝试写一个这样的服务:
1.
module Services {
export class MyService {
getGpsPeople(): Array<AppCommon.GPSPerson> {
var gpsPeople = new Array<AppCommon.GPSPerson>()
for (var i = 0; i < 10; i++) {
var tempPerson = new AppCommon.GPSPerson({ name: "username" + i.toString() });
gpsPeople.push(tempPerson);
}
return gpsPeople;
}
} // MyService class
}
-
这样的控制器:
模块 AppCommon { 出口类控制器{ 范围:ng.IScope;
constructor($scope: ng.IScope) { this.scope = $scope; } }} 模块控制器 {
export interface IMyScope extends ng.IScope { gpsPeople: Array<AppCommon.GPSPerson>; } export class MyController extends AppCommon.Controller { scope: IMyScope; static $inject = ['$scope','myService']; constructor($scope: IMyScope,service:Services.MyService) { super($scope); $scope.gpsPeople = service.getGpsPeople(); } }}
3.GPSPerson 类如下:
export class GPSPoint {
latitude = 0;
longtitude = 0;
constructor(la: number, lg: number) {
this.latitude = la;
this.longtitude = lg;
}
}
export interface IPerson {
name: string;
}
export class GPSPerson
{
name: string;
lastLocation: GPSPoint;
countFlag = 1;
historyLocations: Array<GPSPoint>;
timerToken: number;
startTracking() {
this.timerToken = setInterval(
() => {
var newGpsPoint = null;
var offside = Math.random();
if (this.countFlag % 2 == 0) {
newGpsPoint = new GPSPoint(this.lastLocation.latitude - offside, this.lastLocation.longtitude - offside);
}
else {
newGpsPoint = new GPSPoint(this.lastLocation.latitude + offside, this.lastLocation.longtitude + offside);
}
this.lastLocation = newGpsPoint;
this.historyLocations.push(newGpsPoint);
console.log(this.countFlag.toString() + "+++++++++++++++++++" + this.lastLocation.latitude.toString() + "----" + this.lastLocation.longtitude.toString());
this.countFlag++;
}
, 10000);
}
stopTracking() {
clearTimeout(this.timerToken);
}
constructor(data: IPerson) {
this.name = data.name;
this.lastLocation = new GPSPoint(123.2, 118.49);
this.historyLocations = new Array<GPSPoint>();
}
}
问题是:
1.我应该让 GPSPerson 类成为控制器吗?
2.setinterval 有效,但 UI 没有改变(当我点击按钮时,它改变了,按钮什么也不做)?
我是ts和angular的初学者,对js没有经验,不知道有没有解释清楚,希望有人能帮帮我,谢谢!
【问题讨论】:
标签: angularjs dependency-injection typescript