什么是AngularJS优雅地失败AJAX当没有接入的最佳方式? [英] What is the best way to gracefully fail in AngularJS when no access to AJAX?

查看:125
本文介绍了什么是AngularJS优雅地失败AJAX当没有接入的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有上有一些DIV领域的AngularJS网站。

每一个领域都通过AJAX饲料中获取数据。

根据不同的人的权利登录,部分地区将能得到的数据和其他人则不会。

什么是优雅地显示一条消息,最好的方式还是只隐藏,如果该区域是不允许的相关数据,它需要表现出特定区域?

我想就不会有这样的事:

HTML:

 < D​​IV CLASS =contentWrapperNG-秀=userHasAccess()>
...
< / DIV>

JavaScript的:

  app.controller('customerController',函数($范围,$ HTTP){
    $ scope.userHasAccess =功能(){
        $ http.get(...);
    }
});


解决方案

这个功能将在她最好的情况下,或没有在另一个返回一个承诺对象

一个更清洁的解决方案是:

事业部:

 < D​​IV CLASS =contentWrapperNG-秀=userHasAccess>
...
< / DIV>

控制器:

  app.controller('customerController',函数($范围,$ HTTP){
    $ scope.userHasAccess = FALSE;
    $ scope.getAccess =功能(){
        $ http.get(...)。成功(功能(结果){
               $ scope.userHasAccess =真;
        })错误(功能(错误){
               $ scope.userHasAccess = FALSE;
        });;
    }
});

I have an AngularJS site which has a number of DIV areas on it.

Each of these areas gets data via an AJAX feed.

Depending on the rights of the person logged in, some areas will be able to get data and others won't.

What is the best way to gracefully show a message or just hide the specific area if that area is not allowed to get its data that it needs to show?

I'm thinking there would be something like this:

HTML:

<div class="contentWrapper" ng-show="userHasAccess()">  
...
</div>

JavaScript:

app.controller('customerController', function ($scope, $http) {
    $scope.userHasAccess = function () {
        $http.get(...);
    }
});

解决方案

This function will return a promise object in her best case or nothing in another

A cleaner solution would be:

Div:

<div class="contentWrapper" ng-show="userHasAccess">  
...
</div>

Controller:

app.controller('customerController', function ($scope, $http) {
    $scope.userHasAccess = false;
    $scope.getAccess = function () {
        $http.get(...).success(function(result) {
               $scope.userHasAccess = true;
        }).error(function(error) {
               $scope.userHasAccess = false;
        });;
    }
});

这篇关于什么是AngularJS优雅地失败AJAX当没有接入的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆