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

时间:2015-05-12 13:48:13

标签: javascript php angularjs service controller

在PHP中你可以说2 "Hello" "world" function () 但是如何在PHP方式中选择AngularJS(JavaScript)。

目前我的服务是:

SELECT "message" FROM "users" WHERE id = $id

我的控制器是这样的:

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

这是HTML:

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

这是一项简单的测试,我看到了如何显示<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服务中的用户。

我不知道该怎么做。

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

3 个答案:

答案 0 :(得分:1)

您可以使用ng-if比较users.userId and messages.userId并根据用户显示它们

这是工作的plunker

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

希望这有助于!!!!

答案 1 :(得分:0)

以下是一些数据,向您展示如何使用javascripts Array.prototype.filter根据参数从数组中获取数据。

&#13;
&#13;
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);
    }
  ]);
&#13;
<!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>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

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

如果要对javascript数组进行查询,请考虑使用javascript array

的方法