0

I'm developing a forum using AngularJS and interfacing MySQL with a WS API. My idea is when I user replies I should get the reply and his/her name.

App View

This is my DB MER

MER DB Model

As you can see,

  • One tema(topic) can have many respuesta(replies).
  • One persona(users) can have many respuesta(replies) and also many tema(topics).

Now, In forum controller, I'm getting with $http two responses:

  • url2 -> get replies of the topic filtering with id(from $routeParams) = id_tema(on db)
  • url3 -> get users in general, and should be filtered in forum.html, in order to get the name of the user which is repliying the topic.

forum-controller.js

angular.module('rhoAppApp')
  .controller('temaCtrl', function ($routeParams, $scope, $http) {


    var id = $routeParams.id;

    var url = 'http://server/api.php/tema?filter=id,eq,' + id;

                 $http.get(url).then(function (response) {

                    $scope.titulo = response.data.tema.records[0][1];
                    $scope.texto = response.data.tema.records[0][2];

                })

    var url2 = 'http://server/api.php/respuesta?filter=id_tema,eq,' + id;

                 $http.get(url2).then(function (response) {

                    $scope.respuestas = response.data.respuesta.records;

                })

    var url3 = 'http://server/api.php/persona'

                 $http.get(url3).then(function (response) {

                    $scope.personas = response.data.persona.records;

                })    

  });

Now, I'm triying to map url2 with url3 using filters.

persona[0] (user id) should be equal to respuesta[3], in order two get user name persona[3].

So I followed the next question Map two Collections (Inner Join) and I did something similar in the HTML:

<table class="table">
            <tbody> 
                <tr><th>{{titulo}}</a></th></tr>
                <tr><td>{{texto}}</td></tr>
                <tr ng-repeat="respuesta in respuestas | orderBy : 'respuesta[0]'" id="{{respuesta[0]}} " ><td>
                Por  <span ng-repeat="persona in personas | filter:{persona[0]:respuesta[3]}"> {{persona[3]}} </span>, el {{respuesta[2] | badDateToISO | date:'shortDate'}} a las {{respuesta[2] | badDateToISO | date: "HH:mm"}} Hrs.
                <br>
                <br>
                {{respuesta[1]}}
                </td></tr>
                <tr><td>
                <form >
                <textarea class="form-control" rows="5" required></textarea>
                <br>
                <button type="button" class="width-35 center-block btn btn-sm btn-primary" ng-click="urlbtn()"> 
                Responder
                </button>
                </form>
                </td></tr>
            </tbody>
        </table>

My problem is with this expression filter:{persona[0]:respuesta[3]}, it seems to be that AngularJS doesn't accept 2 arrays expression at the same filter. Also, I have used 'persona[0]:respuesta[3]', but didn't work.

I got this error with this filter:{persona[0]:respuesta[3]}

angular.js:14110 Error: [$parse:syntax] Syntax Error: Token '[' is unexpected, expecting [}] at column 27 of the expression [personas | filter:{persona[0]:respuesta[3]}] starting at [[0]:respuesta[3]}].

the other, I didn't get errors, but also I didn't get the user name.

1 Answer 1

0

After some more research, I found this map array to another array

And I replace filter for ng-if

 <span ng-repeat="persona in personas" ng-if="respuesta[3] === persona[0]" > {{persona[3]}} {{persona[4]}} {{persona[5]}}</span>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.