2017-07-10 59 views
-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。我应该怎么做才能使它工作?

+1

您可能要检查是否'this'实际上是你认为它是在foreach回调函数内... – CBroe

回答

1

当您使用function() {}进行回调时,其内部的上下文(this)根据调用的方式而变化。要保留您的回调中正确的上下文(即RestaurentInfoController实例作为this),使用arrow functions

angular.forEach(this.foodTruckList, (item) => { 
    // ... 
    console.log(this.restaurentId); // `this` will point to current `RestaurentInfoController` instance here 
});