顶级函数的访问参数

时间:2017-10-23 02:58:13

标签: javascript

我想要顶级函数的访问参数。这是我的代码:

 function(data){ // i want access this argument

 logger.info(data.Description,data.Title,clickNotiCustomer)
})

function clickNotiCustomer(){ //here is function run when I click 
                     $http.post('/feeds/clearOneNoti',{id:data._id}).then(()=>{
                        window.location.href = data.Link;
                    })

}

但它不起作用。我收到此错误:未定义数据。我该怎么解决这个问题。请帮帮我

1 个答案:

答案 0 :(得分:1)

鉴于top函数是ajax回调,您可以将变量保存在全局范围的变量中,以便从第二个函数访问它。即:

全球范围:

var ajaxData;

第一个功能:

function(data){
  logger.info(data.Description,data.Title,clickNotiCustomer)

  // assign global variable to data
  ajaxData = data;
}

第二功能:

$http.post('/feeds/clearOneNoti',{id:data._id}).then(()=>{
  // accessing global variable ajaxData
  window.location.href = ajaxData.Link;
})

有关详情,请查看variable scope

相关问题