Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular TypeError: Cannot create property on string ''

Tags:

angularjs

Trying to clear the input box after Add is clicked, like the guy is doing in this tutorial.
I have recreated the error without using the API, with this short code.
You can also check out the Plunker.

HTML

<input ng-model="contact.name" type="text">
<button ng-click="Add()">Add</button>

<ul ng-repeat="contact in contactList">
    <li>{{ contact.name }}</li>
</ul>

JS

$scope.contactList = [
    {name: 'cris'}, {name: 'vlad'}
;]

$scope.Add = function() {
    $scope.contactList.push($scope.contact)
    $scope.contact = ""
}

It seems that i can add 1 item, but on the second i get this Error: enter image description here

like image 534
Cristian Muscalu Avatar asked Jan 24 '17 15:01

Cristian Muscalu


1 Answers

You didn't clean your contact object the right way. In the Add() function, change:

$scope.contact = ""

To:

$scope.contact = {};

Forked Plunker

like image 100
Mistalis Avatar answered Sep 21 '22 15:09

Mistalis