为什么我的ng-hide不能用于失败的json请求?
<div ng-controller="MainController">
<div ng-hide="onError">
Name: {{ user.name}}
Location: {{ user.location}}
Image: <img ng-src="{{user.avatar_url}}" />
</div>
</div>
var MainController = function ($scope, $http) {
var onUserComplete = function (response) {
$scope.user = response.data;
}
var onError = function (reason) {
$scope.error = "Could not fetch the user";
return true;
}
$http.get("https://api.github.com/users/robconery")
.then(onUserComplete, onError);
}
答案 0 :(得分:1)
如果填充div
,我会将您的$scope.error
改为隐藏:
<div ng-hide="error">
您的onError
函数可以保持原样,但不需要返回true。它可以填充$scope.error
,你的div会隐藏,因为$scope.error
是真的:
var onError = function (reason) {
$scope.error = "Could not fetch the user";
}