How to convert data from ng-repeat to variable?

0
<div ng-repeat="item in students">
    <h2>{{item.student_name}}</h2>
</div>

<div ng-repeat="item in courses| filter:{studen_id: item.student_id}">
    <h2>{{item.course_title}}</h2>
</div>

What I want to do is show only the courses in which the student is subscribed.

    
asked by anonfidusa 08.04.2017 в 20:33
source

1 answer

1

Just add ng-repeat of courses within ng-repeat of students

<div ng-repeat="item in students">
   <h2>{{item.student_name}}</h2>
    <div ng-repeat="item in courses| filter:{studen_id: item.student_id}">
      <p>{{item.course_title}}</p>
   </div>
</div>

You can also filter the courses from Javascript , using filter , returning the method some applied to people to verify that the id are equal, both the id Saved in Cursos and Estudiantes .

Example

var app = angular.module('Modulo', []);

app.controller('Controlador', function($scope) {
  $scope.personas = [
    {nombre : 'Jack', cursoid:1},
    {nombre : 'Jack1', cursoid:2},
    {nombre : 'Jack2', cursoid:3},
    ];
  $scope.cursos = [
    {nombre : 'Curso1', cursoid:1},
    {nombre : 'Curso2', cursoid:7},
    {nombre : 'Curso3', cursoid:3},
    ];

    $scope.test = $scope.cursos.filter(function (o) {
    	return $scope.personas.some(function (i) {
        	return i.cursoid == o.cursoid; 
    });
 });
});
<script data-require="[email protected]" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.js" data-semver="1.1.5"></script>
<div ng-app="Modulo">
  	<div ng-controller="Controlador"> 
	   <div ng-repeat="item in personas">
		<h2>{{item.nombre}}</h2>
      </div>
	<div ng-repeat="item1 in test">
		<h3>{{item1.nombre}}</h3>
	 </div>
</div>
    
answered by 08.04.2017 / 21:47
source