angularjs - AngularJS范围变量在其他功能的页面加载中未定义
问题描述
我试图在第一个函数的页面加载中通过http get请求初始化范围变量,但是当尝试在同一页面加载的其他函数中使用范围变量时,它是未定义的。
app.controller('GradeAndSectionCtrl', function ($scope, $http) {
$scope.GetCategories = function () {
$http({
method: 'GET',
url: '/GradeAndSection/GetCategories'
}).then(function (response) {
$scope.categories = response.data;
if (response.data != null) {
$scope.drpCategory = $scope.categories[0].categoryID;
}
});
};
$scope.GetGrades = function () {
\\$scope.drpCategory; here; is; undefined;
$http({
method: 'GET',
url: '/GradeAndSection/GetGrades?categoryID=' + $scope.drpCategory
}).then(function (response) {
$scope.grades = response.data;
});
};
$scope.GetCategories();
$scope.GetGrades();
});
解决方案
您正在代码中使用 Promise 进行异步调用,因此在调用 GetGrades 函数时可能不会加载 $scope.drpCategory。解决 GetCategories 后,您可以调用 GetGrades 函数。
$scope.GetCategories = function () {
$http({
method: "GET",
url: "/GradeAndSection/GetCategories"
}).then(function (response) {
$scope.categories = response.data;
if (response.data != null) {
$scope.drpCategory = $scope.categories[0].categoryID;
$scope.GetGrades();
}
});
}
推荐阅读
- stata - 如果与另一行中的条目相同,则 Dummy 等于 1
- python - 创建一个对输入透明的 Qt 全屏窗口
- reactjs - 如何查询 GraphQL 本地状态以从嵌套结构中检索具有 @client 变量特定值的所有对象
- api - MailChimp API v3.0 - 无法更新现有成员的标签
- java - 字符串不等式
- flutter - Flutter:另一个屏幕中的BottomAppBar导航
- c# - PHImageManager 最大目标大小在 Xamarin iOS 中不起作用
- javascript - 如何使用jquery在DOM之后返回一个按钮
- nginx - 删除 proxy_pass 之前的所有标头(User-Agent 除外)
- sql - 给定表 R1 的结果是什么?