$scope.loadDistrict=function(id)
{
$scope.districtList=[];
angular.forEach($scope.districts,function(district, callback)
{
if(district.district_id==id)
{
$scope.districtList.push(district);
$scope.lat_wgs = district.lat_wgs;
$scope.long_wgs = district.long_wgs;
console.log($scope.lat_wgs);
console.log($scope.long_wgs);
}
})
};大家好,我是第一次接触angular.My的问题,我想访问这个函数外部的变量$scope.lat_wgs和$scope.long_wgs。但是当我运行代码时,我得到了未定义的代码。
发布于 2015-12-09 23:45:40
您需要在函数之前声明它们
$scope.lat_wgs = '';
$scope.long_wgs = '';
$scope.loadDistrict=function(id)
{
$scope.districtList=[];
angular.forEach($scope.districts,function(district, callback)
{
if(district.district_id==id)
{
$scope.districtList.push(district);
$scope.lat_wgs = district.lat_wgs;
$scope.long_wgs = district.long_wgs;
console.log($scope.lat_wgs);
console.log($scope.long_wgs);
}
})
};如果在{中括号}内声明变量,则此变量的值仅在此括号内已知。例如
foo = function(){
var one = 1;
};
console.log(one); //oh no, i don't know what is value of 'one' variable
//well, I will output undefined解决方案-在{方括号}前声明变量
var one = 1;
foo = function(){
one = 2;
};
console.log(one); //it will output 2, cause value of variable 'one' changed
//inside foo function, but it won't be undefinedhttps://stackoverflow.com/questions/34182884
复制相似问题