AngularJS check if promise is empty or not

淺唱寂寞╮ 提交于 2020-01-07 09:35:11

问题


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')
        }

    });

回答1:


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')
}


来源:https://stackoverflow.com/questions/32445789/angularjs-check-if-promise-is-empty-or-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!