2014-10-01 77 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; 

    }); 
相關問題