TypeError:无法设置属性&#39; <value>&#39;为null

时间:2016-02-22 09:11:36

标签: angularjs

我的控制器中有以下功能。我在调用服务之前对值进行了硬编码,如下所示。它会在第一行抛出TypeError: Cannot set property 'mag_id' of null at r.$scope.subscribeToMag

它甚至没有接到服务电话,所有的帮助表示赞赏。如果我没有将变量实例化为null,则会抛出不同的错误。

$scope.newSubscription=null;

$scope.subscribeToMag = function(){
    $scope.newSubscription.mag_id = 2;
    $scope.newSubscription.user_id = 6;
    $scope.newSubscription.status = "E";
    $scope.newSubscription.end_date = "2016-02-19"    

    hobbyService.subscribeMag($scope.newSubscription)
        .then(
            function(response){
                $scope.subscribeResult=response;
                console.log($scope.subscribeResult);
            },
            function(err){
                console.log('error subscribing to the mag: ', err);
            }                 
        );
}

1 个答案:

答案 0 :(得分:2)

您将$scope.newSubscription实例化为null,但在您的函数中,您将其视为对象:

$scope.newSubscription.mag_id

如果您将代码更改为此代码,则应该有效:

$scope.newSubscription = {};