Add new text box on click of a button in angular js

前端 未结 2 1436
Happy的楠姐
Happy的楠姐 2021-01-22 20:18

How do I add new text box when submit button is pushed. This is what I have tried and I know there\'s some thing wrong. I am still new to angular js.

Example:

         


        
2条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-22 20:25

    In you code, you are doing:-

            $scope.addContact = function()
            {
              $scope.contact.push({$scope.contact})
            }
    

    This is where the problem is. If you just need to add an empty textbox, you just need to add an empty contact to contacts, and ng-repeat will take care of adding the new textbox in the list. Another thing is that you were pushing into contact, and not contacts.

    Also, ngSubmit is used inside a form, which you do not have. So, use ng-click instead. Here is a plunker showing the code working.

                $scope.addContact = function()
                {
                  $scope.contacts.push({ name: '' });
                }
    

提交回复
热议问题