如何从具有ID的两个服务中获取数据并显示内容

How to get data from two services with an ID and display the content

在PHP中你可以说SELECT "message" FROM "users" WHERE id = $id但是我如何在AngularJS(JavaScript)中select像PHP那样.

目前我的服务如下:

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"
                }
            ]
        }
    ]
});

而我的控制器是这样的:

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>

这是我进行的一个简单测试,目的是了解如何向 users 服务中的用户显示来自 userMessages 服务和 link 的消息。

我不知道该怎么做。

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

可以用ng-if比较users.userId and messages.userId,根据用户显示

这是工作中的 plunker

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

希望对您有所帮助!!!!

这里有一些数据向您展示如何使用 javascripts Array.prototype.filter 根据参数从数组中获取数据。

angular.module('app', [])
  .service('users', function() {
    this.userList = [{
      userId: 1,
      username: "John",
      password: "Doe"
    }, {
      userId: 2,
      username: "Jane",
      password: "Doe"
    }];
    this.text = function() {
      return "Hello";
    };
  })
  .service('userMessages', function() {
    var messages = [{
      userId: 1,
      messages: [{
        text: "This is a message from user 1"
      }, {
        text: "This is another message from user 1"
      }]
    }, {
      userId: 2,
      messages: [{
        text: "This is a message from user 2"
      }, {
        text: "This is another message from user 2"
      }]
    }];

    this.getUsersMessages = function(id) {
      return messages.filter(function(obj, i) {
        return obj.userId == id;
      });
    }
  })
  .controller('testCtrl', ['$scope', 'users', 'userMessages',
    function($scope, users, userMessages) {
      $scope.user1 = users.userList[0];
      $scope.user2 = users.userList[1];
      
      $scope.user1Messages = userMessages.getUsersMessages($scope.user1.userId);
      $scope.user2Messages = userMessages.getUsersMessages($scope.user2.userId);
    }
  ]);
<!DOCTYPE html>
<html>

<head>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
  <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet" />
  <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.3.0/css/font-awesome.css" rel="stylesheet" />
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>

<body ng-app="app">
  <div ng-controller="testCtrl">
    {{user1Messages}}
    <br />
    {{user2Messages}}
  </div>
</body>

</html>

SELECT * FROM * WHERE 与 PHP 无关,它是 SQL 数据库语言,如 MySQL.

当您想查询 javascript 数组时,请考虑使用 javascript array.

的方法