Angular 为什么我的函数只返回数组中的第一个对象
我正在为学生组织者web应用程序编写笔记模块。我写了一个函数来获取一个对象的ID,但它不能正常工作,或者我的代码的另一部分有问题,但我无法解决它 此函数应返回类别和主题的名称。不知何故,它只返回category&subject数组中的第一个名称 我的尝试:Angular 为什么我的函数只返回数组中的第一个对象,angular,typescript,Angular,Typescript,我正在为学生组织者web应用程序编写笔记模块。我写了一个函数来获取一个对象的ID,但它不能正常工作,或者我的代码的另一部分有问题,但我无法解决它 此函数应返回类别和主题的名称。不知何故,它只返回category&subject数组中的第一个名称 我的尝试: 我在一个没有视图的空白项目中测试了该函数。没问题 HTML/Angular-表单和视图 我选择一个类别和一个主题。注释类别有不同的选项。1和2带有截止日期和主题输入字段,3只是标题和文本。这是我的HTML/Angular表单和视图 <i
我在一个没有视图的空白项目中测试了该函数。没问题 HTML/Angular-表单和视图
我选择一个类别和一个主题。注释类别有不同的选项。1和2带有截止日期和主题输入字段,3只是标题和文本。这是我的HTML/Angular表单和视图
<input type="text" name="title" [(ngModel)] = "Title" placeholder="Titel">
<select name="Category" [(ngModel)] = "Category">
<option *ngFor="let category of Categorys" value="{{category._ID}}">{{category._Name}}</option>
</select>
<div [ngSwitch]="Category">
<div *ngSwitchCase="1">
<select name="subject" [(ngModel)] = "Subject">
<option *ngFor="let subject of Subjects" value="{{subject._ID}}">{{subject._Name}}</option>
</select>
<input type="text" name="deadline" [(ngModel)] = "Enddate" placeholder="Deadline eintragen">
</div>
<div *ngSwitchCase="2">
<select name="subject" [(ngModel)] = "Subject">
<option *ngFor="let subject of Subjects" value="{{subject._ID}}">
{{subject._Name}}</option>
</select>
<input type="text" name="deadline" [(ngModel)] = "Enddate" placeholder="Deadline eintragen">
</div>
</div>
<hr>
<div *ngIf="Category == 1 || Category == 2 || Category == 3">
<input type="text" name="note" [(ngModel)] = "Note" placeholder="Notiz">
</div>
<hr>
<button (click) = "add()">Save me</button>
<hr>
<div class="noteBox">
<h1>Notizen </h1>
<div class="note" *ngFor="let note of Notes">
<h2>{{note._Title}}</h2>
<span>{{note._DateCreated}} | {{note._CategoryName}}</span>
<span>Fach: {{note._SubjectName}} | Ablaufdatum: {{note._Enddate}}</span>
<hr>
<article>
{{note._Content}}
</article>
</div>
</div>
如果有用的话:这是我的数据。因为我没有数据源,所以数据是硬编码的
constructor() {
this.addSubjects();
this.addCategorys();
}
addSubjects = function () {
this.Subjects.push(new Subject(89, 'math'));
this.Subjects.push(new Subject(16, 'german'));
this.Subjects.push(new Subject(45, 'english'));
this.Subjects.push(new Subject(89, 'ped'));
};
addCategorys = function () {
this.Categorys.push(new Category(45, 'homework'));
this.Categorys.push(new Category(13, 'reminder'));
this.Categorys.push(new Category(27, 'fastnote'));
};
完整的app.component.ts文件:如果第一个
id
不等于objArray
中的第一个元素,则for循环将退出,因为循环中有一个返回“undefined”
我认为正确的方法是将return语句移出循环
findMyObjectByID = function (objArray, id) {
for (const obj of objArray) {
if (+obj._ID === +id) {
console.log(obj._Name);
return obj._Name;
}
}
return 'undefined';
}
谢谢你的回答,但那没用。我修改了函数,发现如果添加第二个注释,“obj”对象值的属性不会改变。在你的粘贴库中,你仍然有
返回'undefined'代码>在循环内,因此如果第一个元素不是正确的ID,您将始终返回“undefined”。你需要把它放在圈外。您的循环应该查找数组中的每个元素,只有在它查找了所有元素而没有找到您要查找的元素时,才返回“undefined”。看这里,好吧,我错了。现在一切正常。谢谢:)
constructor() {
this.addSubjects();
this.addCategorys();
}
addSubjects = function () {
this.Subjects.push(new Subject(89, 'math'));
this.Subjects.push(new Subject(16, 'german'));
this.Subjects.push(new Subject(45, 'english'));
this.Subjects.push(new Subject(89, 'ped'));
};
addCategorys = function () {
this.Categorys.push(new Category(45, 'homework'));
this.Categorys.push(new Category(13, 'reminder'));
this.Categorys.push(new Category(27, 'fastnote'));
};
findMyObjectByID = function (objArray, id) {
for (const obj of objArray) {
if (+obj._ID === +id) {
console.log(obj._Name);
return obj._Name;
}
}
return 'undefined';
}