2017-06-05 65 views
0

我有一个存储对一个未定义的数字形式:标签/值角2追加点击按钮

<form (submit)=new_answers_for_this_question()> 
<div class="row"> 
    <div class="input-field col m5"> 
     <input placeholder="Label" id="label" type="text" class="validate"> 
    </div> 
    <div class="input-field col m5"> 
     <input placeholder="Value" id="values" type="text" class="validate"> 
    </div> 
    <div class="input-field col m2 center-align"> 
     <button class="btn-floating waves-effect waves-light" (click)="new_answer()"><i class="material-icons">add</i></button> 
    </div> 
</div> 
<button>Insert Answers</button> 
</form> 

我只是想填补这两个输入,然后按add button添加一个新行用新的标签/值input,之前的add button会消失,并且新增一个新按钮来追加新的可能的行。使用jQuery很简单,因为append父div上的整行div,但使用角度我不知道如何处理这个问题,¿任何想法?

编辑:其目的是为DataBase中相同问题的每对标签和值插入一行。

即:

**Question 1** 
Label: 'good', value: '7-10' 
Label: 'neutral', Value: '4-6' 
Label: 'bad', Value: '1-3' 
+0

使用循环('* ngFor')在用户按下“添加”时添加项目的数组上。 – 2017-06-05 12:26:01

+0

但第一次我需要一个输入,第一次循环时将是空的,然后没有输入显示 – PriNcee

回答

0

试试这个

angular.module('submitExample', []) 
 
    .controller('ExampleController', ['$scope', function($scope) { 
 
     $scope.list = []; 
 
     
 
     $scope.submit = function() { 
 
     if ($scope.text) { 
 
      $scope.list.push(this.text); 
 
      $scope.text = ''; 
 
     } 
 
     }; 
 
    }]);

 

 
<form ng-submit="submit()" ng-controller="ExampleController"> 
 
    Enter text and hit enter: 
 
    <input type="text" ng-model="text" name="text" /> 
 
    <input type="submit" id="submit" value="Submit" /> 
 
    <pre>{{list}}</pre> 
 
</form>

+0

问题是询问Angular,而不是AngularJS。 – 2017-06-05 12:25:24

+0

经过这个:http://plnkr.co/edit/fictP28Vqn74YqrwM1jW?p = preview –

+0

我想要输入以前的标签/值对,因为可能用户想改变它,所以必须连接模型我猜:/ – PriNcee

0

在这里你可以编辑和保存

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
import { Component } from '@angular/core'; 
 

 
class ContactInfo { 
 
    constructor(public description: string) {} 
 
} 
 

 
@Component({ 
 
    selector: 'my-app', 
 
    template: ` 
 
    <ul> 
 
    <li *ngFor="let info of information"> 
 
     <pre>{{ info.description }}</pre> 
 
    </li> 
 
    </ul> 
 
    
 
    <input #newInfo (keyup.enter)="addInfo(newInfo.value)" (onclick)="addInfo(newInfo.value); newInfo.value='' "> 
 
    
 
    <button (click)="addInfo(newInfo.value)">Add</button> 
 
    ` 
 
}) 
 
export class AppComponent { 
 
    information = []; 
 
    
 
    myInfo = this.information[0]; 
 
    
 
    addInfo(newInfo: string) { 
 
    if (newInfo) { 
 
     this.information.push(new ContactInfo(newInfo)); 
 
    } 
 
    } 
 

 
}

+0

我试图更新你的coment,无论如何这里是一个plunkr的代码:http://plnkr.co/edit/9YSs9txF6wR8ogX7PTdY?p=preview – PriNcee