2016-10-26 24 views
1

私はテーブルにTODOを追加するコンポーネントを持っています。新しいTODOを底面ではなくテーブルの最上部に追加する方法を知りたかったのです。私はJSでこれを達成する方法を示すSOにいくつかの答えを見ましたが、私はTypescriptでそれを行う方法を知りたがっていました。どんな助けでも大歓迎です、ありがとう! Typescriptangular 2新しいテーブル項目をテーブルの先頭に挿入

+0

あなたは提出DESCの日時によって一覧を注文することができます。あなたがコードを投稿すると、今すぐ手助けすることができます – 11mb

+0

unshiftを使って配列/リストの先頭に新しい要素を追加することもできます。 – chanoto89

+0

タイコスクリプトとはどういう意味ですか?スプライス関数は、typescriptでのみ使用してください。 – micronyks

答えて

3

http://plnkr.co/edit/7FC8IUvdR3wPevrd48yt?p=preview

<div> 
    <input type="text" [(ngModel)]="newTodo" placeholder="Add New Todo"/> 
    <button type="button" (click)="addTodo()">Add</button> 
</div> 
<div> 
    <ul> 
    <li *ngFor="let todo of todos">{{todo}}</li> 
    </ul> 
</div> 


export class App { 
    newTodo: string; 
    todos: Array<string>; 

    constructor() { 
    this.newTodo = ""; 
    this.todos = ["Grocery Shopping", "Banking"]; 
    } 

    addTodo() { 
    this.todos.unshift(this.newTodo); 
    this.newTodo = ""; 
    } 
} 
2

splice機能は、あなたが欲しいものを行うために使用することができます。

DEMO:あなたはちょうどあなたの配列の初めに項目を追加するために使用することができますアンシフトhttps://plnkr.co/edit/B5l1fCOvItkt45cJMBKO?p=preview

<input type="text" #newItem> 
<button (click)="add(newItem.value)">Add</button> 

<table> 
    <tr *ngFor="let item of items"> 
     <td>{{item.name}}</td> 
    </tr> 
</table> 

export class App { 

    items=[{name:"Computer"},{name:"Laptop"}]; 
    add(newItem){ 
    this.items.splice(0,0,{name:newItem}); 
    } 
} 
関連する問題