2014-10-01 52 views
0

的财产我有一个手表的功能,做一些基本的数学与被输入的值。无法设置为NULL对象

$scope.$watch('currentItem.JobOriginalContract -- currentItem.JobTotalCO', function (value) { 
    $scope.currentItem.JobRevisedContract = value; 
}); 

TypeError: Cannot set property 'JobRevisedContract' of null

当我做到这一点...

$scope.currentItem.JobRevisedContract = null; 
$scope.currentItem.JobRevisedContract = {}; 
$scope.$watch('currentItem.JobOriginalContract -- currentItem.JobTotalCO', function (value) { 
    $scope.currentItem.JobRevisedContract = value; 
}); 

...错误消息消失,但它会导致一切不工作。它没有错误。我能做什么?

回答

2

检查对象试图在其上设置属性之前就存在:

if (!$scope.currentItem) $scope.currentItem = {}; 
$scope.currentItem.JobRevisedContract = value; 
+0

好,那工作完美感谢你,不能接受的答案。但也许你可以帮我解决类似的问题。它还没有解决。 http://stackoverflow.com/questions/25790184/how-to-correct-this-typeerror-cannot-read-property-of-undefined – texas697 2014-10-01 19:29:09

1

试试这个

$scope.$watch('currentItem.JobOriginalContract -- currentItem.JobTotalCO', function (value) { 
    $scope.currentItem.JobRevisedContract={}; 
    $scope.currentItem.JobRevisedContract = value; 

    });