我需要一些指导,弄清楚如何在两个不同的表中的角度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;
}]);
Profile Controller代码段:
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 function”的值,但它是学校名称和详细信息的完整列表,而不是与用户匹配的名称和详细信息。
答案 0 :(得分:0)
我认为这是服务器端逻辑。当您将登录用户传递给api时,然后根据服务器端的该用户执行一些数据库查询,并返回包含所有学校列表的json对象。
答案 1 :(得分: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;
}
});
});
}