gpt4 book ai didi

javascript - HTML 变量值未传递给 html 标记中的 app.controller 属性

转载 作者:行者123 更新时间:2023-11-30 19:10:06 26 4
gpt4 key购买 nike

我正在开发一个基本的学生 - 教师 Web 应用程序,我正在使用 Angular JS 构建前端。我的 JS 文件中有 2 个应用程序 Controller ,一个用于检索学生,另一个用于检索分配给每个学生的科目。

我在将学生 ID 作为属性 {{student.id}} 传递给应用程序 Controller 时遇到问题,因为它只是作为字符串“{{student.id}}”传递,而不是传递实际值{{student.id}}

出现问题的 html 代码片段

<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>

我遇到的问题是在这一行

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

像这样运行同一行代码似乎按预期工作,但我希望能够在 for 循环中迭代所有学生 ID

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

有没有人对我如何克服这个问题有任何建议。我在网上广泛查看但找不到解决方案,因为大多数教程和文档都侧重于通过 ng-model 接收用户输入。

我对编程还很陌生,所以非常感谢任何帮助。

更新

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

});

最佳答案

subjectController 应该从其父作用域继承属性:

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

有关详细信息,请参阅

关于javascript - HTML 变量值未传递给 html 标记中的 app.controller 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58582612/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com