使用Javascript将值分配给数组时遇到问题

时间:2015-02-21 00:02:46

标签: javascript angularjs

好的,是的,我知道这是基本的东西,但它让我知道了什么。

以下是代码:

myApp.controller('keyExpController', function ($scope, KeyExpDataService) {
    var ctrlExp = this;
    ctrlExp.keyExp = [];
    $scope.company = {};
    ctrlExp.achKeys = {
        company: [{
                achieves: [],
                details: {
                    super: "",
                    whyleft: ""

                }
            }]
    };
    $scope.keyachievements = [];
    $scope.compName = null;

    ctrlExp.fetchKeyExp = function () {
        //Resume Data
        KeyExpDataService.getKeyExpData().then(function (result) {

            ctrlExp.keyExp = result.data.resume.proexperience;
            console.log("Result: " + ctrlExp.keyExp);
            $scope.groupBy(ctrlExp.keyExp.length);
        });
    };
    ctrlExp.fetchKeyExp();

    // I group the friends list on the given property.
    $scope.groupBy = function (nbrComps) {

        //Set the global value for number of companies
        nbrCompanies = nbrComps;

        var compValue = "_INVALID_GROUP_VALUE_";

        for (var i = 0; i < nbrComps; i++) {

            $scope.keyachievements = ctrlExp.keyExp[i].keyachievements;

            if (ctrlExp.keyExp[i].companyat !== compValue) {

                $scope.company = [{
                        achievements: [],
                        details: {
                            companyName: ctrlExp.keyExp[i].companyat,
                            super: ctrlExp.keyExp[i].supervisor,
                            whyleft: ctrlExp.keyExp[i].reasonforleaving
                        }
                    }];

                compValue = $scope.company.companyName;
                $scope.compName = compValue;

//It's HERE, with the first line that I continually get the following error:
//TypeError: Cannot read property 'achieves' of undefined
//at Scope.$scope.groupBy (controllers.js:151)

//This is line 151 just below:
                achievements.company[i].achieves[i] = $scope.keyachievements;
                achievements.company[i].details.super = ctrlExp.keyExp[i].supervisor;
                achievements.company[i].details.whyleft = ctrlExp.keyExp[i].reasonforleaving;

                ctrlExp.achKeys.company[i].achieves[i] = $scope.keyachievements;
                ctrlExp.achKeys.company[i].details.super = ctrlExp.keyExp[i].supervisor;
                ctrlExp.achKeys.company[i].details.whyLeft = ctrlExp.keyExp[i].reasonforleaving;
            }
        }
    };
});

现在我正在做的事情很好,直到我在控制器中点击它。我的OBJECT看起来像是为了简洁而突破:

    ctrlExp.achKeys = {
        company: [{
                achieves: [],
                details: {
                    super: "",
                    whyleft: ""

                }
            }]
    };

我无法弄清楚,对于我的生活,是的,它必须是因为我超过50岁,为什么我不能将任何东西分配给对象的第一部分:

此:     ctrlExp.achKeys.company [0] .achieves [0] = $ scope.keyAchievements

$ scope.keyAchievements包含特定公司的所有项目符号。一个人的简历上可能有“n”个公司。因此,我正在通过这些公司来获取公司的名称和“关键成就”,这些成就位于该公司的“下”之下。

这就是它。

我会发布“服务”,但这个网站是我自己网站的剪辑,尚未发布,我将用作未来考虑的模板。一旦我明白了,我就是金色的。

感谢大家对我们工艺的贡献。

3 个答案:

答案 0 :(得分:1)

看起来它试图将值设置为尚未定义的变量,即achievements.company[i]

所以,如果你添加了类似的内容:

achievements.company[i]={};

在第151行之前,它可以正常工作。好吧,你不会得到

  

TypeError:无法读取属性&#39>实现&#39;未定义的

消息。


在JavaScript中,您可以使用未定义的变量/对象,但不能为它们指定属性。


从上面的错误消息中,看起来achievements已定义,而achievements.company[i]未定义,因此当您想要为其分配achieves[i]属性时会出现此问题。

如果achievements未定义,您将收到消息:

  

无法阅读财产&#39;公司&#39;未定义的

答案 1 :(得分:0)

您使用什么浏览器来测试它?我问,因为我过去曾遇到过类似命令的不兼容问题,所以我现在就避开它们。

相反,我会做更兼容的Array.push()更改:

ctrlExp.achKeys.company[0].achieves[0] = $scope.keyAchievements

ctrlExp.achKeys.company[0].achieves.push($scope.keyAchievements);

答案 2 :(得分:0)

所以,是的,在我发布问题之后,我明白了。

我是这样做的:

myApp.controller('keyExpController', function ($scope, KeyExpDataService) {
    var ctrlExp = this;
    ctrlExp.keyExp = [];
    $scope.company = {};
    ctrlExp.achKeys = {
        company: {
            achieves: [],
            details: []
        }
    };
    $scope.keyachievements = [];
    $scope.compName = null;

    ctrlExp.fetchKeyExp = function () {
        //Resume Data
        KeyExpDataService.getKeyExpData().then(function (result) {

            ctrlExp.keyExp = result.data.resume.proexperience;
            console.log("Result: " + ctrlExp.keyExp);
            $scope.groupBy(ctrlExp.keyExp.length);
        });
    };
    ctrlExp.fetchKeyExp();

    // I group the friends list on the given property.
    $scope.groupBy = function (nbrComps) {

        //Set the global value for number of companies
        nbrCompanies = nbrComps;

        var compValue = "_INVALID_GROUP_VALUE_";

        for (var i = 0; i < nbrComps; i++) {

            $scope.keyachievements = ctrlExp.keyExp[i].keyachievements;

            if (ctrlExp.keyExp[i].companyat !== compValue) {

                $scope.company = {
                    achievements: [$scope.keyachievements],
                    details: [{
                            companyName: ctrlExp.keyExp[i].companyat,
                            super: ctrlExp.keyExp[i].supervisor,
                            whyleft: ctrlExp.keyExp[i].reasonforleaving
                        }]
                };

                compValue = $scope.company.companyName;
                $scope.compName = compValue;

                achievements.company.achieves[i] = $scope.keyachievements;
                details.company.hr[i] = [
                    {
                        super: ctrlExp.keyExp[i].supervisor,
                        whyleft: ctrlExp.keyExp[i].reasonforleaving
                    }
                ];

                ctrlExp.achKeys.company.achieves[i] = $scope.keyachievements;
                ctrlExp.achKeys.company.details[i] = [
                    {
                        super: ctrlExp.keyExp[i].supervisor,
                        whyleft: ctrlExp.keyExp[i].reasonforleaving
                    }
                ];

            }
        }
    };
});

在所有控制器调用之上,我把这些ARRAYS放在全局范围内:

var achievements = {
    company: {
        achieves: []
    }
};
var details = {
    company: {
        hr: []
    }
};

var edu1 = {
    hsandcol: {
        school: []
    }
};
var edu2 = {
    other: {
        school: []
    }
};

这是一个示例服务,我只需复制,粘贴并重命名,因为我需要

    myApp.factory('EduDataService', function ($http, URL) {

        var getEduExpData = function () {
            return $http.get(URL + 'resume.json')
                    .success(function (data) {
                        console.log("SUCCESS!");
                        //console.log("The Key Experiences array length: " + data.resume.proexperience.length);
                        return data.resume.education;
                    })
                    .error(function (e) {
                        console.log("He\'s dead Jim!", e);
                        return e;
                    });

        };
        return {
            getEduExpData: getEduExpData
        };

    });

在HTML文件中,我想出了如何在控制器中隔离和嵌入控制器,调用父控制器在子控制器获取所有数据时执行所有操作。

非常酷。

注意:ctrlRes是来自PARENT CONTROLLER的调用:resumeController方式。我将在最后的网站完成发布。它非常棒,JSON对象包含了一个人简历的所有内容,对于任何需要ANGULAR和Bootstrap核心简历网站的人来说,它都是一个很棒的模板。实际上,这对我而言并不介意分享,但它会暴露我的姓氏。

和HTML代码段:

    <div class="my-education">
        <h3>My Education</h3>
        <!-- Education Start -->
        <div class="education" ng-controller="eduController">
            <h4>High School and College</h4>
            <div class="panel-group" id="accordionedu{{$index}}">
                <div class="panel panel-default" ng-repeat="edu in ctrlRes.eduHsCol[0]">
                    <div class="panel-heading">
                        <h5 class="panel-title">
                            <a data-toggle="collapse" data-parent="#accordion" href="#collapseEdu{{$index}}">
                                {{edu.name}}&nbsp;{{edu.type}}
                                <span class="pull-right">[{{edu.start}} - {{edu.end}}]</span>
                            </a>
                        </h5>
                    </div>
                    <div id="collapseEdu{{$index}}" class="panel-collapse collapse">
                        <div class="panel-body">
                            <div>
                                <h5>
                                    <span class="pull-left">
                                        <strong>Location:</strong>
                                        &nbsp;{{edu.locale}}
                                    </span>
                                    <span class="pull-right">
                                        <strong>Major:</strong>
                                        &nbsp;{{edu.maj}}
                                    </span>
                                </h5>
                                <br><br>
                                <span class="pull-left">
                                    <strong>Graduated:</strong>
                                    &nbsp;{{edu.grad}}&nbsp;
                                    <strong>GPA:</strong>
                                    &nbsp;{{edu.gpa}}
                                </span>
                                <span class="pull-right">
                                    <strong>Minor:</strong>
                                    &nbsp;{{edu.min}}
                                </span>
                                <br>
                                <hr/>
                                <p>
                                    <strong><em>Comments:&nbsp;</em></strong>
                                </p>
                                <div>
                                    {{edu.comments}}
                                </div>
                                <br>
                                <div ng-show="edu.type === 'College'">
                                    <a href="documents/Academic Transcript.pdf" target="_blank">
                                        Download Academic Transcript - PASSWORD PROTECTED
                                    </a>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <pre hidden="hidden">{{ctrlRes.eduHsCol|json}}</pre>
            </div>
            <!-- Education 1 End -->
        </div>
        <pre hidden="hidden">{{ctrlRes.eduHsCol|json}}</pre>
        <pre hidden="hidden">{{ctrlRes.eduOther|json}}</pre>
    </div>