CoolLife
CoolLife

Reputation: 1479

Cannot create property '' on string '' angularjs

I need to add rows and inputs dynamically, in addition to filling each entry in these fields, but at the moment of wanting to fill the input I get a error, with this add the rows:

    $scope.detalleTransCover = {
        details: []
    };
    $scope.addDetail = function () {
        $scope.detalleTransCover.details.push('');
    };
    $scope.submitTransactionCobver = function () {
        angular.forEach($scope.detalleTransCover, function(obj)
        {
           console.log(obj.cuenta);
        });
    };

now in the html:

<tr ng-repeat="detail in detalleTransCover.details">
    <td>
        <input type="text" class="form-control" ng-model="detail.cuenta">
    </td>
    <td>
        <input type="text" class="form-control" ng-model="detail.debeDolar">
    </td>
    <td>
        <input type="text" class="form-control" ng-model="detail.haberDolar">
    </td>
</tr>

<button ng-click="addDetail()" class="btn btn-block btn-primary">
    Agregar fila
</button>
<button ng-click="submitTransactionCobver()" class="btn  btn-block btn-primary">
    Agregar
</button>

on the html when I try to fill the input example "cuenta" haver error:

TypeError: Cannot create property 'cuenta' on string ''

Upvotes: 0

Views: 5436

Answers (1)

Pop-A-Stash
Pop-A-Stash

Reputation: 6652

When you push a new input to your array, you need to push an object not a string:

// wrong
$scope.addDetail = function () {
    $scope.detalleTransCover.details.push('');
};

// right
$scope.addDetail = function () {
   $scope.detalleTransCover.details.push({})
});

Strings can't have properties the same way objects can.

Upvotes: 1

Related Questions