1

I am working on a basic student - teacher web application where I'm building the front-end using angular JS. I have 2 app controllers in my JS file one for retrieving the students and the other retrieving the subjects allocated to each student.

I am having an issue passing the student id to the app controller as an attribute {{student.id}} as it is just being passed as the string "{{student.id}}" rather than passing actual value of {{student.id}}

Snippet of html code where issue occurs

<div ng-controller="studentController" class="container">

    <table class="table">
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Subjects</th>
        </tr>
        <tr ng-repeat="student in students">
            <td id>{{student.id}}</td>
            <td>{{student.Name}}</td>
            <td ng-controller="subjectController"
                id = {{student.id}}>
             {{subjects}}
            </td>
        </tr>
    </table>

The issue I am having is at this line

<td ng-controller="subjectController" id = {{student.id}}> {{subjects}}</td>

Running the same line of code like this seems works as expected but I want to be able to iterate over all student ID's in the for loop

<td ng-controller="subjectController" id = 1> {{subjects}}</td>

Does anyone have any suggestions on how I can overcome this issue. I looked extensively online but couldn't find a solution as the majority of tutorials and documentation focus on taking in user input through ng-model.

I am fairly new to programming so any help would be highly appreciated.

UPDATE

app.controller('studentController', function($scope, $location, $http) {
    $http.get('http://localhost:8080/getStudents')
    .then(function(response) {
        $scope.students = response.data;
    });

});

app.controller('subjectController', function($scope, $attrs, $location, $http) {
    $http.get('http://localhost:8080/getSubjects?ID='+ $attrs.id)
            .then(function(response) {
                $scope.subjects = response.data;
            });

});

1
  • It seems odd that you need an AngularJS controller for one <td> element. If you insist on doing such, please show the code for that contoller so that we can help you with it. Commented Oct 27, 2019 at 20:39

1 Answer 1

1

The subjectController should inherit properties from its parent scopes:

app.controller("subjectController", function($scope) {
    console.log($scope.student.id); 
    console.log($scope.students); //Inherited from parent scope
});

For more information, see

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a million! This is exactly what I was trying to achieve. My approach was completely off as I wasn't utilizing access to the parent scope in the JS file.

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.