如何纠正此TypeError无法读取undefined的属性

时间:2014-09-11 14:31:13

标签: angularjs

我的布局页面上有一个表,其中包含一系列作业。我正在使用rootscope和sessionStorage来保持在网站上选择的活动。每个工作都有变更订单。当我突出显示作业时,我会单击changeOrder视图。属于该作业的changeOrders出现在表中。当我双击其中一个打开模态没有任何反应,我收到错误信息。但是,当我在作业表上选择一个新作业时,它将起作用。所以这是最初尝试不起作用。 错误消息

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) 

控制器

//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(); };`

布局页面主要作业表

<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

更新代码

//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); });

1 个答案:

答案 0 :(得分:0)

就像typmeJV所说的那样,$ rootScope.job为空。

你可以这样做:

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

但这仍然闻起来很糟糕?如果$ rootScope中缺少作业对象,则可能会发生错误。

为什么要把这份工作放在$ rootScope上?