-1
未定義
我的代碼如下所示:公共變量變成在功能打字稿
/// <reference path="../../../typings/app.d.ts" />
/// <reference path="../../../typings/tsd.d.ts" />
module App.Controller {
import Services = Core.Services;
import Shared = Core.Shared;
export class RestaurentInfoController extends BaseController {
public restaurentName: any = [];
public checkBox: any;
public restaurent: any;
public foodTruckList: any = [];
public foodCategories: any = [];
public drinkCategories: any = [];
public restaurentId : any;
static $inject: Array<string> = ['baseAppService', 'userAuthorizationService', 'storageService', 'eventService',];
constructor(
appService: Services.BaseAppService
, public userAuthorizationService: Services.UserAuthorizationService,
public storageService: Services.StorageService,
public eventService: Services.AppEventBusService) {
super(appService);
this.getRestaurentList();
}
routeTo(view) {
this.appService.routerService.routeToPage(view);
}
getRestaurentList =(): void => {
this.appService.networkService.get<any>(this.appService.appConstant.appUrls.getFoodTruckName).then((response) => {
this.foodTruckList = response.data;
},
(error) => { });
}
changeStatus =(): void => {
if (this.checkBox === '1') {
this.getFoodCategories();
}
else if (this.checkBox === '2') {
this.getDrinkCategories();
}
}
getFoodCategories =(): void => {
console.log("rest " + this.restaurent);
angular.forEach(this.foodTruckList, function (item) {
console.log("here" + item.foodtruck_name);
if(item.foodtruck_name === 'world in a box') {
console.log("match res "+ this.restaurent + " " + item._id);
this.restaurentId = item._id;
console.log("ressss "+ this.restaurentId);
}
});
console.log("restaurentId "+this.restaurentId);
this.appService.networkService.get<any>(`${this.appService.appConstant.appUrls.getFoodCategories}/${this.restaurentId}`).then((response) => {
this.foodCategories = response.data;
console.log('popuar Items Loaded', this.foodCategories);
},
(error) => { });
}
getDrinkCategories =(): void => {
var data = {
_id: this.restaurent._id
}
this.appService.networkService.get<any>(this.appService.appConstant.appUrls.getFoodTruckName, data).then((response) => {
this.foodTruckList = response.data;
console.log('popuar Items Loaded', this.foodTruckList);
},
(error) => { });
}
}
}
這裏發生的事情是this.restaurentId
正顯示出與ressss
。但不知怎麼的console.log值,該值變得不確定時,控制檯.log會打印restaurentId
。我應該怎麼做才能使它工作?
您可能要檢查是否'this'實際上是你認爲它是在foreach回調函數內... – CBroe