Angularjs 公共变量在typescript中的函数中变得未定义
我的代码如下所示:Angularjs 公共变量在typescript中的函数中变得未定义,angularjs,typescript,Angularjs,Typescript,我的代码如下所示: /// <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 RestaurentInf
/// <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) => { });
}
}
}
//
///
模块应用控制器{
导入服务=核心服务;
导入共享=Core.Shared;
导出类RestaurentInfoController扩展BaseController{
公共餐厅名称:any=[];
公共复选框:任何;
公共餐厅:任何;
公共食品目录:any=[];
公共食品类别:任何=[];
公共饮料类别:任何=[];
公共餐厅ID:任何;
静态$inject:Array=['baseAppService','userAuthorizationService','storageService','eventService',];
建造师(
appService:Services.BaseAppService
,public userAuthorizationService:Services.userAuthorizationService,
公共存储服务:Services.storageService,
公共事件服务:服务。AppEventBussService){
超级(应用服务);
此文件为.getRestaurentList();
}
路线图(视图){
this.appService.routerService.routeToPage(视图);
}
getRestaurentList=():void=>{
this.appService.networkService.get(this.appService.appConstant.appurl.getFoodTruckName)。然后((响应)=>{
this.foodTruckList=response.data;
},
(错误)=>{});
}
changeStatus=():void=>{
如果(this.checkBox==“1”){
这个.getFoodCategories();
}
else if(this.checkBox==“2”){
这个.getDrinkCategories();
}
}
getFoodCategories=():void=>{
console.log(“rest”+this.restarent);
angular.forEach(this.foodTruckList,函数(项){
console.log(“here”+item.foodtruck_name);
如果(item.foodtruck_name===‘盒子里的世界’){
console.log(“match res”+this.restaurant+“”+item.\u id);
this.restaurentId=项目。\u id;
console.log(“resss”+this.restaurentId);
}
});
console.log(“restarentid”+此.restarentid);
this.appService.networkService.get(`${this.appService.appConstant.appurl.getFoodCategories}/${this.restaurentId}')。然后((响应)=>{
this.foodCategories=response.data;
log('popuar Items Loaded',this.foodCategories);
},
(错误)=>{});
}
getDrinkCategories=():void=>{
风险值数据={
_id:这是一家餐厅
}
this.appService.networkService.get(this.appService.appConstant.appurl.getFoodTruckName,数据)。然后((响应)=>{
this.foodTruckList=response.data;
log('popuar Items Loaded',this.foodTruckList);
},
(错误)=>{});
}
}
}
这里发生的是
这一点。restaurentId
显示带有ressss
的console.log的值。但不知何故,打印带有restaurentId
的console.log时,该值变得未定义。我应该怎么做才能使它工作?当您对回调使用function(){}
时,它内部的上下文(this
)会根据调用方式发生变化。要在回调中保留正确的上下文(即,RestaurentInfoController
实例作为this
),请使用:
您可能需要检查此是否实际上是您认为它在forEach回调函数中的内容。。。
angular.forEach(this.foodTruckList, (item) => {
// ...
console.log(this.restaurentId); // `this` will point to current `RestaurentInfoController` instance here
});