2014-08-28 85 views
0

为什么我的隐藏在失败的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); 
} 

回答

1

我会改变时$scope.error填充像这样你div,而不是藏着掖着

<div ng-hide="error"> 

onError功能可以保持原样,但你并不需要返回true。它可以只填充$scope.error和您的div将隐藏,因为$scope.error是真实的:

var onError = function (reason) { 
    $scope.error = "Could not fetch the user"; 
} 
相关问题