AngularJS 检查承诺是否为空 [英] AngularJS check if promise is empty or not

查看:28
本文介绍了AngularJS 检查承诺是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码是为了检查数据库中是否有用户,如果发现在视图上显示用户列表,否则显示具有用户创建形式的视图,但没有进行检查表达我做错了什么

Hi I have this code for the purpose of checking if there are users in the database, if it finds shows a list of users on a view, otherwise shows a view with form of user creation, but didn't work the check expression what I'm doing wrong

users = Users.query();

users.$promise.then(
    function (data) {
        if (!data) {
            $location.url('/NewUser')
        } else {
            $location.url('/UsersList')
        }

    });

推荐答案

如果没有找到,它可能返回一个空数组.尝试检查数据长度.

It is probably returning an empty array in case nothing is found. Try checking the data length.

if (data.length == 0) {
   $location.url('/NewUser')
} else {
   $location.url('/UsersList')
}

这篇关于AngularJS 检查承诺是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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