我的代码中的工厂方法为: -
(function () {
var app = angular.module('MyApp', []);
app.controller('MYController', function ($scope, CourseDescFactory) { // here $scope is used for share data between view and controller
$scope.MyDescCourses = [];
CourseDescFactory.OurCourses().then(function (d) {
$scope.MyDescCourses = d.data;
$scope.MainHeading = $scope.MyDescCourses[0].CD.MainHeading;
$scope.MainDesc = $scope.MyDescCourses[0].CD.MainDesc;
$scope.MainImg = $scope.MyDescCourses[0].CD.Img;
$scope.MySubDescCourses = [];
$scope.MySubDescCourses = $scope.MyDescCourses[0].CSD;
//$scope.Img = $scope.MyDescCourses[0].Img;
}, function (error) {
alert('Cannot Load Course Detail');
});
})
.factory('CourseDescFactory', function ($http) {
var CourseDescList = {};
CourseDescList.OurCourses = function (id) {
return $http({
method: 'GET',
url: '/Home/CourseDesc',
params: {}
});
}
return CourseDescList;
});
})();
当我从后端获取数据时,此功能正常。但是当没有数据时,而不是显示警报('无法加载课程详细信息');它会引发错误'无法读取属性' CD'未定义的'。
任何帮助将不胜感激。
答案 0 :(得分:1)
最好检查一下您的响应变量" data"并且内容包含以下命令。
if (
(d.data != undefined) && (d.data != null) && (d.data != "")
)