我对 AngularJs 完全陌生,我有这个我不明白的问题。我有两种方法。第一个从 web 服务中获取一些数据并放入范围内定义的变量中。但是当我想在第二种方法中使用该变量时,它是未定义的。有人可以帮助我理解为什么会发生这种情况并提供解决方案吗?
var myApp= angular.module( "myApp", [] );
myApp.controller("myAppController",
function( $scope ) {
$scope.getAll = function(){
$.ajax({
type: "GET",
dataType: "jsonp",
contentType: "application/json; charset=utf-8",
url: ..something...,
success: function (parameters) {
$scope.profiles = angular.copy(parameters); <-- correct data is returned
$scope.$apply();
},
error: function () {
alert("Error calling the web service.");
}
});
}
$scope.getCategories = function(){
var all = $scope.profiles; <-- At this point profiles is empty
...
}
$scope.getAll();
$scope.getCategories();
}