2017-02-15 44 views
0

我需要一些指導來確定如何在兩個不同的表中爲角度js創建兩個相似的值。我使用的表格是用戶表格和學校表格。所以我需要做的是基於登錄的用戶,我應該得到一個與該用戶相關的學校名稱,但目前我不知道如何去做這件事。任何幫助指導我如何去做這件事將不勝感激。如何使用angularjs匹配來自兩個不同數據庫的項目

學校服務控制器代碼:

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功能」值正常返回,但學校名稱的整個列表和細節而不是與用戶相匹配的細節。

回答

0

我認爲這是服務器端邏輯。當你將登錄用戶傳遞給你的api時,在服務器端根據該用戶做一些數據庫查詢,並返回包含所有學校列表的json對象。

+0

是的,這就是它是如何工作至今。我只是主要與預製控制器進行交互。我還是有點新的角,所以我想了解如何循環我的數據,以查找用戶表詳細信息是否與學校名稱相關的學校表詳細信息,因爲當前用戶表只顯示學校表ID,而不是名字。 –

0

我終於想出了一種方法,可以根據用戶表中學校的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; 
      } 
     }); 
    }); 
} 
相關問題