如何纠正这种类型错误无法读取的未定义的属性 [英] How to correct this TypeError Cannot read property of undefined

查看:2005
本文介绍了如何纠正这种类型错误无法读取的未定义的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有作业列表我的布局页上的表。我使用rootscope和sessionStorage的保持什么都通过了该网站选定活跃。每个职业都有changeOrders。当我强调一个工作,我再点击changeOrder视图。属于该作业的changeOrders上出现的表。当我双击其中一个打开模式没有任何反应,我得到的错误信息。然而,当我选择的作业表的新工作将随后工作。因此,它是,最初的尝试是行不通的。
错误消息

 类型错误:无法读取的未定义的属性作业名
在范围$ scope.editChangeOrderModal。(HTTP://本地主机:44301 / MyScripts / JobController.js:186:40)
在http://本地主机:44301 /脚本/ angular.js:10836:21
在http://本地主机:44301 /脚本/ angular.js:19094:17
在范围$的eval(HTTP://本地主机:44301 /脚本/ angular.js:12673:28)。
在适用范围适用于$(HTTP://本地主机:44301 /脚本/ angular.js:12771:23)。
在HTMLTableCellElement<&匿名GT; (HTTP://本地主机:44301 /脚本/ angular.js:19093:21)
在HTMLTableCellElement.x.event.dispatch(HTTP://本地主机:44301 /脚本/ jQuery的-1.10.2.min.js:22:14129)
在HTMLTableCellElement.v.handle(HTTP://本地主机:44301 /脚本/ jQuery的-1.10.2.min.js:22:10873)

控制器

  //编辑ChangeOrder莫代尔
$ scope.currentItem = NULL;
$ scope.editChangeOrderModal =功能(模型){
    $ scope.JobName = $ rootScope.job.JobName;
    $ scope.JobId = $ rootScope.job.JobId;
    $ scope.currentItem =模型;
    $ ekathuwa.modal({
        ID:editChangeOrderModal,contentStyle:宽度:800像素; heigth:400像素,
        适用范围:$范围,
        templateURL:ModalEditChangeOrder
    });
}`
 //同步表选择/的sessionStorage
$ scope.selectedJob = $ $ sessionStorage的默认($ scope.jobArray [1])。
$ scope.selectJob =功能(作业){$ rootScope.job =工作; angular.extend($ scope.selectedJob,工作); };
$ scope.clearSelectedJob =函数(){$ $ sessionStorage的复位()。 };`

布局网页的主要工作表

 <表类=表表bordred表条纹表悬停>
                    < TR><第i否LT; /第i<第i个姓名和LT; /第i< / TR>
                    < TR NG重复=作业jobArray级=指针NG-CLASS ={亮点:job.JobNumber === selectedJob.JobNumber}>
                        < TD NG-DBLCLICK =editJobModal(工作)NG点击=selectJob(工作)> {{job.JobNumber}}< / TD>
                        < TD NG-DBLCLICK =editJobModal(工作)NG点击=selectJob(工作)> {{job.JobName}}< / TD>
                    < / TR>
                < /表>
            < / DIV><! - 结束作业TABL

更新code

  //编辑ChangeOrder莫代尔
$ scope.currentItem = NULL;
$ scope.editChangeOrderModal =功能(模型){
    如果($ rootScope.hasOwnProperty('工作')及和放大器;!$ rootScope.job = NULL){
        $ scope.JobName = $ rootScope.job.JobName;
        $ scope.JobId = $ rootScope.job.JobId;
    }
    //$scope.JobName = $ rootScope.job.JobName;
    //$scope.JobId = $ rootScope.job.JobId;
    $ scope.currentItem =模型;
    $ ekathuwa.modal({
        ID:editChangeOrderModal,contentStyle:宽度:800像素; heigth:400像素,
        适用范围:$范围,
        templateURL:ModalEditChangeOrder
    });
}//找到工作
$ scope.jobArray = {};
JobGet.query(),然后(功能(数据){$ scope.jobArray =数据;}函数(原因){errorMngrSvc.handleError(原因);});


解决方案

像typmeJV说,$ rootScope.job为null。

您可以这样做:

 如果($ rootScope.hasOwnProperty('工作')及和放大器;!$ rootScope.job = NULL){
    $ scope.JobName = $ rootScope.job.JobName;
    $ scope.JobId = $ rootScope.job.JobId;
}

但是,这仍然味道不好?如果工作对象是从$ rootScope失踪不好的事情可能会发生。

你为什么把作业上的$ rootScope?

I have a table on my layout page with a list of jobs. i am using rootscope and sessionStorage to keep what ever is selected active through out the site. Every job has changeOrders. When I highlight a job I then click on the changeOrder view. the changeOrders that belong to that job appear on a table. when I double click on one of them to open the modal nothing happens and I get the error message. However when I select a new Job on the jobs table it will then work. So it is that initial try that does not work. Error Message

TypeError: Cannot read property 'JobName' of undefined
at Scope.$scope.editChangeOrderModal (http://localhost:44301/MyScripts/JobController.js:186:40)
at http://localhost:44301/Scripts/angular.js:10836:21
at http://localhost:44301/Scripts/angular.js:19094:17
at Scope.$eval (http://localhost:44301/Scripts/angular.js:12673:28)
at Scope.$apply (http://localhost:44301/Scripts/angular.js:12771:23)
at HTMLTableCellElement.<anonymous> (http://localhost:44301/Scripts/angular.js:19093:21)
at HTMLTableCellElement.x.event.dispatch (http://localhost:44301/Scripts/jquery-1.10.2.min.js:22:14129)
at HTMLTableCellElement.v.handle (http://localhost:44301/Scripts/jquery-1.10.2.min.js:22:10873) 

Controller

//Edit ChangeOrder Modal
$scope.currentItem = null;
$scope.editChangeOrderModal = function (model) {
    $scope.JobName = $rootScope.job.JobName;
    $scope.JobId = $rootScope.job.JobId;
    $scope.currentItem = model;
    $ekathuwa.modal({
        id: "editChangeOrderModal", contentStyle: "width:800px;heigth:400px",
        scope: $scope,
        templateURL: "ModalEditChangeOrder"
    });
}` 
 //Sync Table Selections / sessionStorage
$scope.selectedJob = $sessionStorage.$default($scope.jobArray[1]);
$scope.selectJob = function (job) { $rootScope.job = job; angular.extend($scope.selectedJob,    job); };
$scope.clearSelectedJob = function () { $sessionStorage.$reset(); };`

Layout page Main Job table

<table class=" table table-bordred table-striped table-hover">
                    <tr><th>No</th><th>Name</th></tr>
                    <tr ng-repeat="job in jobArray" class="pointer" ng-class="{highlight: job.JobNumber===selectedJob.JobNumber}">
                        <td ng-dblclick="editJobModal(job)" ng-click="selectJob(job)">{{job.JobNumber}}</td>
                        <td ng-dblclick="editJobModal(job)" ng-click="selectJob(job)">{{job.JobName}}</td>
                    </tr>
                </table>
            </div><!--End Job Tabl

Updated Code

//Edit ChangeOrder Modal
$scope.currentItem = null;
$scope.editChangeOrderModal = function (model) {
    if ($rootScope.hasOwnProperty('job') && $rootScope.job != null) {
        $scope.JobName = $rootScope.job.JobName;
        $scope.JobId = $rootScope.job.JobId;
    }
    //$scope.JobName = $rootScope.job.JobName;
    //$scope.JobId = $rootScope.job.JobId;
    $scope.currentItem = model;
    $ekathuwa.modal({
        id: "editChangeOrderModal", contentStyle: "width:800px;heigth:400px",
        scope: $scope,
        templateURL: "ModalEditChangeOrder"
    });
}

//GET Jobs
$scope.jobArray = {};
JobGet.query().then(function (data) { $scope.jobArray = data; }, function (reason) { errorMngrSvc.handleError(reason); });

解决方案

Like typmeJV said, $rootScope.job is null.

You can do:

if ($rootScope.hasOwnProperty('job') && $rootScope.job != null) {
    $scope.JobName = $rootScope.job.JobName;
    $scope.JobId = $rootScope.job.JobId;
}

But this still smells bad? If the job object is missing from the $rootScope bad things may happen.

Why are you putting the job on the $rootScope?

这篇关于如何纠正这种类型错误无法读取的未定义的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆