我有一个JS对象,我用它来保存我网站上某些数据的当前状态,以便稍后在另一个函数中使用。数据保存在一个函数中,然后从另一个函数访问。
$scope.object = {"name": " ", "age": " ", "phone" : " " }
$scope.setData = function() {
$scope.object.name = "Alex";
$scope.object.age = "23";
$scope.object.phone = "123-456-4321"
//Data will output here just fine
console.log($scope.object.name)
console.log($scope.object.age)
console.log($scope.object.phone)
}
$scope.getData = function() {
//No data will print here after I have set the object. Why is this?
console.log($scope.object.name)
console.log($scope.object.age)
console.log($scope.object.phone)
}
$scope.setData();
$scope.getData();
问题是,当我尝试从另一个函数访问对象的数据时,它无法读取我在原始函数中保存的数据。我认为这可能与我没有正确访问当前范围有关,但我不是100%肯定。我习惯了vanilla JS,因为它在全局范围内,所以我可以访问这个对象。所有帮助表示赞赏!