gpt4 book ai didi

javascript - 如何通过ID从两个服务获取数据并显示内容

转载 作者:行者123 更新时间:2023-12-03 10:09:36 25 4
gpt4 key购买 nike

在 PHP 中,你可以说 SELECT "message"FROM "users"WHERE id = $id 但是我如何才能像 PHP 那样在 AngularJS (JavaScript) 中进行选择。

以下是我目前的服务:

app.service("users", function() {
this.userList = [
{
userId: 1,
username: "John",
password: "Doe"
},
{
userId: 2,
username: "Jane",
password: "Doe"
}
];
this.text = function() {
return "Hello";
};
});

app.service("userMessages", function() {
this.messages = [
{
userId: 1,
message: [
{
text: "This is a message from user 1"
},
{
text: "This is another message from user 1"
}
]
},
{
userId: 2,
message: [
{
text: "This is a message from user 2"
},
{
text: "This is another message from user 2"
}
]
}
]
});

我的 Controller 是这样的:

app.controller("controller", function($scope, users, userMessages) {
$scope.users = users.userList;

var id = $scope.users.length

$scope.add = function() {
$scope.users.push(
{
userId: ++id,
username: $scope.username,
password: $scope.password
}
);
$scope.username = "";
$scope.password = "";
};
});

这是 HTML:

<div class="container" ng-controller="controller">
<div ng-repeat="user in users">
{{ user.userId }}<br />
{{ user.username }}<br />
{{ user.password }}<br />
<hr />
</div>
<input type="text" ng-model="username" placeholder="Username"><br />
<input type="text" ng-model="password" placeholder="Password"><br />
<button ng-click="add()">Add user</button>
</div>

这是我进行的一个简单测试,目的是了解如何显示来自 userMessages 服务的消息并将它们链接到 users 服务中的用户。

我不知道该怎么做。

我做了一些研究,但找不到解决方案。任何帮助将不胜感激。

最佳答案

可以使用ng-if比较users.userId和messages.userId并根据用户显示

这是工作中的笨蛋

http://embed.plnkr.co/39Dd6AtKorcxX24fNmJF/preview

希望这有帮助!!!!

关于javascript - 如何通过ID从两个服务获取数据并显示内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30192851/

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