gpt4 book ai didi

javascript - MEAN Stack 从每一行的 UserId 中获取用户名

转载 作者:行者123 更新时间:2023-12-03 07:49:38 24 4
gpt4 key购买 nike

我正在尝试获取一行评论中每个用户的用户数据(例如用户名)。我保留了我的用户信息(另一个模型/ Controller 上的登录凭据),那么有没有办法访问并在 View 上显示用户名?

下面是我的 JSON 代码:

{
"_id": "56873fc9182b3741059357d0",
"longitude": 113.83507800000007,
"latitude": 22.1533884,
"location": "Hong Kong",
"name": "Hong Kong",
"__v": 0,
"category": "Attraction",
"reviews": [
{
"comment": "Add the comment to the form",
"rating": 3.5,
"userId": 11,
"review_id": "44NL7kkwhy72"
},
{
"comment": "Hello test",
"rating": "3.4",
"userId": "56809c0cf0a264b101a1dd61",
"review_id": "jN7f1iFlQVha"
},
{
"comment": "Hello test ty",
"rating": "3.7",
"userId": "56863c8f2959b4c601fbd9eb",
"review_id": "QcJpw4yopF1q"
}
]
},

//查看某个位置的所有评论

    .controller('AllReviews', function($scope, $stateParams, LocFac, UserFac) {   

id = $stateParams.id;

LocFac.getLocation(id).then(function(data) {
$scope.locations = data.data;
$scope.reviews = data.data.reviews;

//variables to show information on location reviews
$scope.lengthrev = (data.data.reviews).length;
$scope.locationname = data.data.name;

//addition of values and retrieve the value
$scope.getTotal = function(){
var total = 0;
for(var i = 0; i < $scope.lengthrev; i++){
var review = $scope.reviews[i];

User.getUser(review).then(function(datat) {
$scope.locun = datat.username;
});

total += review.rating;
}
return total;
}

grandtotal = $scope.getTotal();

//get average of all values \
$scope.averagereviews = grandtotal/($scope.lengthrev);
});
})

我的位置评论 View

<ion-view view-title="All Reviews" class="all_reviews">
<ion-content class="all_reviews">

<h3>{{ locationname }}</h3>

<h3>Average Rating: {{ averagereviews }} <ng-rate-it name="rating" ng-model="averagereviews" resetable="false" read-only="true"></ng-rate-it>/ {{ lengthrev }} Reviews </h3>

<ion-list>
<ion-item data-ng-repeat="location in locations.reviews">
<ng-rate-it name="rating" ng-model="location.rating" resetable="false" read-only="true"></ng-rate-it>
{{ location.userId }}
<h4>{{ location.review_id }}</h4>
<h4>{{ location.comment }}</h4>
</ion-item>
</ion-list>

最佳答案

考虑到您的两个 aysnc 操作(一个用于获取评论,另一个用于获取用户名),我认为这种方法会更合适。

  1. 首先获取评论。
  2. 显示评论。
  3. 在显示评论时,使用 UserFac 使用 ng-init 指令异步获取用户名。

我使用您提供的示例数据创建了一个演示插件作为示例。

示例:

Demo Plunker

app.js

var app = angular.module("app", []);
app.factory('LocFac', function($http) {
var factory = {};

factory.getLocation = function(id) {
return $http({
method: 'GET',
url: 'data.json'
});
}

return factory;
});
app.factory('UserFac', function($http) {
var factory = {};

factory.getUser = function(id) {
return $http({
method: 'GET',
url: 'user.json'
});
}

return factory;
});
app.controller('AllReviews', function($scope, LocFac, UserFac) {
$scope.show = false;
$scope.testClick = function() {
id = "56873fc9182b3741059357d0";
LocFac.getLocation(id).then(function(data) {
$scope.reviews = data.data.reviews;
$scope.lengthrev = (data.data.reviews).length;
$scope.show = true;
});
}

$scope.getUserName=function(review) {
UserFac.getUser(review.id).then(function(user) {
review.userName=user.data.userName;
review.showUserName=true;
});
}
})

HTML:

<!DOCTYPE html>
<html>

<head>
<script data-require="angular.js@1.4.8" data-semver="1.4.8" src="https://code.angularjs.org/1.4.8/angular.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>

<body ng-app="app" ng-controller="AllReviews">
<button ng-click="testClick()">Click here</button>
<ul ng-show="show" ng-repeat="review in reviews track by $index" ng-init="getUserName(review)">
{{review | json : spacing}}
<p ng-show="review.showUserName">
{{review.userName}}
</p>
</ul>
</body>

</html>

说明:

在 ng-repeat 中迭代评论数组时,我们将评论对象传递给 UserFac 服务以获取用户名。该服务将在评论对象本身内设置名称。

关于javascript - MEAN Stack 从每一行的 UserId 中获取用户名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35054423/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com