简体   繁体   English

为什么我得到一条消息说forEach不是函数?

[英]Why do I get a message saying forEach is not a function?

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 ) TypeError:su.subjects.forEach不是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. 我在其他地方使用过类似的代码,但每次都失败了。

subjects is an Object , not an Array , because of its {} notation. subjects是一个Object ,而不是一个Array ,因为它有{}符号。 You can loop through 1 and 100 as keys if you like using Object.keys(subjects) . 如果您喜欢使用Object.keys(subjects)可以将1100作为键循环。 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. 您也可以将其作为空数组( [] )启动,然后根据需要设置subjects[1]subjects[100]的值,但是没有简写内联方式来定义数组的两个分离索引而没有介于两者之间。

su.subjects是一个对象,而不是一个数组,而且没有为Object定义forEach

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. 我还要注意, forEach并没有在所有版本的浏览器中实现(看着你,IE 8)因为在ECMA 5.1之前不包含forEach See forEach forEach

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试进行申请时,为什么会收到一条消息,指出“对象不是函数”? - Why do I get a message saying “Object is not a function” when I try to do an apply? 我收到TypeError说forEach不是函数吗? - I am getting TypeError saying forEach is not a function? 为什么我收到一个错误说拆分未定义? - Why do I get an error saying split undefined? 为什么我会在控制台中收到有效的 function 的错误消息? - Why do I get an error message in console for a function that works? 为什么我会为此功能获得双控制台消息? - Why do I get a double console message for this function? 为什么我在 XMLHttpRequest.request.onload 处收到“forEach 不是 function”错误? - Why do i get "forEach is not a function at XMLHttpRequest.request.onload" error? 为什么会收到错误消息,说明我使用ajax创建的服务不是函数 - why did I get an Error saying the service I created with ajax is not a function 为什么当我拨打 http 时,会收到一条错误消息说我的 url 不是字符串? - Why, when I make an http call, do I get an error saying my url is not a string? 为什么我收到此错误消息? - Why do I get this error message? 我不断收到一条错误消息,说 this.replaceState 不是一个函数 - I keep getting an error message saying this.replaceState is not a function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM