为什么我的ng-hide不起作用?

时间:2014-08-28 19:40:43

标签: angularjs

为什么我的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);
}

1 个答案:

答案 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";
}