【问题标题】:How to match item from two different databases based using angularjs如何使用angularjs匹配来自两个不同数据库的项目
【发布时间】:2017-02-15 08:27:47
【问题描述】:

我需要一些指导来弄清楚如何从两个不同的表中在 angular js 中生成两个相似的值。正在使用的表是用户表和学校表。所以我需要做的是基于登录用户我应该得到一个与该用户相关的学校名称,但目前不知道如何去做。在指导我如何进行此操作方面的任何帮助将不胜感激。

学校服务控制器代码:

    angular.module('starter').factory('Schools',['apiUrl','$resource', 'UserInfo', function(apiUrl,$resource, UserInfo){
    var factory = $resource(apiUrl + '/schools/:schoolId', 
    { 
        schoolId: '@_id'
    }, {
        update: {
            method: 'GET'
        }
    });

    return factory;
}]);

配置文件控制器代码片段:

 angular.module('starter.controllers')

.controller('ProfileController', function($scope,$http,$state,Schools, $ionicPopup, apiUrl, UserInfo,$ionicLoading,$ionicHistory,Users,Authentication) {

    $scope.apiUrl = apiUrl;
    $scope.authentication = Authentication;
    $scope.state = $state;
    $scope.selected = {};

    var user = Authentication.user.school;

   $scope.find = function(queryParams){
    $scope.place = [];
    var user = $scope.authentication.user.school;
    var school = Schools.query({_id: user.school})

    var params = queryParams || {};
    params._id = user;

    Schools.query(params, function(response){
        if (params._id === school){
            // response.splice(0, $scope.place.lengh);
            // for(var x = 0; x< response.lengh; x++){
            //  $scope.place.push(response[x]);
            // }
            $scope.place = response[0].name;
        }
        else{
            $scope.place = response;
        }
    })
}

    $scope.signOut = function(){

        $ionicPopup.confirm({
            title: "SIGN OUT",
            template: '<p align="center">' + 'PLEASE CONFIRM IF YOU WANT TO SIGN OUT'+ '</p>',
            buttons: [
            { text: "NO",
            type: 'button button-outline button-assertive',
            onTap:function(e){
                return false;
            }
        },
        {
            text: "YES",
            type:'button-positive',
            onTap:function(e){
            //now you can clear history or goto another state if you need
            $ionicHistory.clearHistory();
            $ionicHistory.nextViewOptions({ disableBack: true, historyRoot: true });

            $state.go('signin');
            return true;

        }
    }
    ]
});
    };
});

“$scope.find 函数”的值已正确返回,但它是学校名称和详细信息的完整列表,而不是与用户匹配的列表。

【问题讨论】:

    标签: angularjs


    【解决方案1】:

    我认为这是服务器端逻辑。当您将登录用户传递给您的 api 时,然后在服务器端根据该用户执行一些数据库查询,并返回带有所有学校列表的 json 对象。

    【讨论】:

    • 是的,到目前为止就是这样。我只是主要与预构建控制器交互。我对 Angular 还是有点陌生​​,所以我试图了解我将如何遍历我的数据以查找用户表详细信息是否与学校名称方面的学校表详细信息相匹配,因为当前用户表仅显示学校表 ID 而不是名字。
    【解决方案2】:

    我终于想出了一种方法,可以根据用户表中学校的 id 引用来获取正确的值。我必须使用 angularjs forEach 方法首先查询数据,然后在 if 语句中对其进行过滤以获得我正在寻找的结果。

    解决办法:

    $scope.find = function(){
        var school = $scope.authentication.user.school;
        $scope.schoolName = Schools.query(function(results) {
            results.forEach(function(result) {
                if (result._id === school) {
                    $scope.schoolName = result.name;
                    return $scope.schoolName;
                }
            });
        });
    }
    

    【讨论】:

      猜你喜欢
      • 2016-11-26
      • 2018-03-25
      • 1970-01-01
      • 2021-04-11
      • 1970-01-01
      • 2022-01-24
      • 1970-01-01
      • 2018-08-14
      • 1970-01-01
      相关资源
      最近更新 更多