-1

im new in Angular and I'm trying to make site with list of books (with properties a,b,c,d) and where u can add new book. 1st time when i submit everythik goes fine and 2nd time i get this error v2.newBook is not a function. Does anyone knows why its working 1st time and 2nd time not? Thx

`https://plnkr.co/edit/OtfLPuQFptfgUxuo0pXJ`
Luke
  • 23
  • 5
  • Welcome to StackOverflow! You will have a much higher chance of receiving an answer to your question if you provide a bit of additional information. In particular, it is important to provide the relevant code to your problem *in the question body*. Links to other websites alone don't provide context to others in the future, and hiding links inside the code blocks makes the link not clickable. Please see http://stackoverflow.com/help/on-topic, #1. – Claies May 15 '16 at 18:04

4 Answers4

2

It's because you use the same name for the function to call and for the book model itself. After initiation only the $scope.newBook function exists, but later on you overwrite that with your book model. Just simply rename either the function or the model.

John Smith
  • 2,291
  • 4
  • 22
  • 33
1

in your code function resetForm() convert $scope.newBook function to object

 $scope.newBook = function(book) {
    book.id = $scope.books.length;
    $scope.books.push(book);
    resetForm();
  }

  function resetForm() {
    $scope.newBook = { //here you defined newBook again as object
      d: '',
      b: '',
      c: '',
      d: ''
    }
  }
aseferov
  • 6,155
  • 3
  • 17
  • 24
0

https://plnkr.co/edit/3wwbDfTjcVQKx0VmsDFn?p=preview


Here is a working plnkr. You have both your function and object as newBook which is causing the problem.


$scope.newBook1 = function(book) {
    book.id = $scope.books.length;
    $scope.books.push(book);
    resetForm();
  }

HTML :

    <form class="form-group" ng-show="newBookForm" ng-submit="newBook1(newBook)" novalidate>

Nikhilesh K V
  • 1,480
  • 13
  • 20
0

You have made a tiny mistake in your code, actually you have reset d object twice and left resetting a. function resetForm() { $scope.newBook = { d: '', b: '', c: '', d: '' } }

just change d:'' to a:'' and it will run.

Thanks