admin管理员组文章数量:1291197
In typescript I defined:
class SubjectService implements ISubjectService {
subject: any;
subjectId: number = 0;
subjects = {
"1": { "id": 1, "name": "Java" },
"100": { "id": 100, "name": "Test" }
};
static $inject = [
"$http",
"appConstant",
];
constructor(
public $http: ng.IHttpService,
public ac: IAppConstant
) {
}
}
I then in my constructor have this code:
class SubjectController {
static $inject = [
"$scope",
"subjectService"
];
constructor(
public $scope,
public su: ISubjectService
) {
$scope.su = su;
$scope.rowClicked = (subject, $index) => {
var self = this;
if (subject.current && subject.current == true) {
return;
}
su.subjects.forEach(function (subject) {
subject.current = false;
});
su.subject = subject;
su.subject.current = true;
}
}
}
But when this runs I am getting a message saying:
TypeError: su.subjects.forEach is not a function at Scope.SubjectController.$scope.rowClicked (http://localhost:1810/app/controllers/SubjectController.js:12:25)
Does anyone have any idea what might be wrong. I used similar code in other places but here it fails each time.
In typescript I defined:
class SubjectService implements ISubjectService {
subject: any;
subjectId: number = 0;
subjects = {
"1": { "id": 1, "name": "Java" },
"100": { "id": 100, "name": "Test" }
};
static $inject = [
"$http",
"appConstant",
];
constructor(
public $http: ng.IHttpService,
public ac: IAppConstant
) {
}
}
I then in my constructor have this code:
class SubjectController {
static $inject = [
"$scope",
"subjectService"
];
constructor(
public $scope,
public su: ISubjectService
) {
$scope.su = su;
$scope.rowClicked = (subject, $index) => {
var self = this;
if (subject.current && subject.current == true) {
return;
}
su.subjects.forEach(function (subject) {
subject.current = false;
});
su.subject = subject;
su.subject.current = true;
}
}
}
But when this runs I am getting a message saying:
TypeError: su.subjects.forEach is not a function at Scope.SubjectController.$scope.rowClicked (http://localhost:1810/app/controllers/SubjectController.js:12:25)
Does anyone have any idea what might be wrong. I used similar code in other places but here it fails each time.
Share Improve this question edited Jul 1, 2015 at 13:34 Cerbrus 73k19 gold badges136 silver badges150 bronze badges asked Jul 1, 2015 at 13:26 Samantha J T StarSamantha J T Star 32.8k89 gold badges256 silver badges441 bronze badges 1- 1 possible duplicate of Why is "forEach not a function" for this map? – Cerbrus Commented Jul 1, 2015 at 13:35
3 Answers
Reset to default 3subjects
is an Object
, not an Array
, because of its {}
notation. You can loop through 1
and 100
as keys if you like using Object.keys(subjects)
. You could also start it out as an empty array ([]
) and then set the values of subjects[1]
and subjects[100]
if you like, but there's no shorthand inline way to just define two separated indices of an array without the inbetween ones.
su.subjects
is an object, not an array and forEach
is not defined for Object
.
I would also note that forEach
is not implemented in all versions of browsers (looking at you, IE 8) since forEach
was not included until ECMA 5.1. See forEach
本文标签: javascriptWhy do I get a message saying forEach is not a functionStack Overflow
版权声明:本文标题:javascript - Why do I get a message saying forEach is not a function? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741514437a2382797.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论