最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Angular將填入表單的數(shù)據(jù)渲染到表格的方法

 更新時間:2017年09月22日 08:28:01   作者:fyk曩昔  
這篇文章主要介紹了Angular將填入表單的數(shù)據(jù)渲染到表格的方法,非常具有實用價值,需要的朋友可以參考下

一、項目簡介

我們將采用Angular框架來做一個demo,這個demo將要實現(xiàn)的功能如下:

在X坐標和Y坐標文本框輸入信息,然后點擊添加,就會在下面表格 中出現(xiàn)一項相應的數(shù)據(jù),點擊每一項旁邊的刪除按鈕,該條信息就會被刪除!

因為我們的表格數(shù)據(jù)是經常刷新的,所以我們把它獨立出來作為一個組件。

二、項目目錄

--------app

----------dataTable(文件夾)

------------dataTable.component.html

------------dataTable.component.css

------------dataTable.component.ts

----------app.component.html

----------app.component.css

----------app.component.ts

----------app.module.ts

三、代碼講解

1.app.component.html

我們先把主體框架寫好

<div class="container">
 <div class="row">
  <form>
   <div class="form-group">
    <label for="exampleInputEmail1">X坐標</label>
    <input type="text" class="form-control" id="exampleInputEmail1" placeholder="xcood" name="xcood">
   </div>
   <div class="form-group">
    <label for="exampleInputPassword1">Y坐標</label>
    <input type="text" class="form-control" id="exampleInputPassword1" placeholder="ycood" name="ycood">
   </div>
   <button type="button" class="btn btn-default" (click)="additem()">添加</button>
  </form>  
 </div>
 <div class="row">
  <data-table [array]="addArray"></data-table><!--導入dataTable組件,并且將父組件里面的form表單數(shù)據(jù)傳遞給子組件渲染-->
 </div>
</div>

這里使用了ngx-bootstrap,文末我們再講解一下如何導入這個東西。

2.app.component.ts

我們再父組件需要用到一個添加功能的additem()方法

import { Component } from '@angular/core';

@Component({
 selector: 'app-root',
 templateUrl: './app.component.html',
 styleUrls: ['./app.component.css']
})
export class AppComponent {
 addArray=[];
 xcood: any;
 ycood: any;

 additem(){
  this.xcood = (document.getElementsByName('xcood')[0] as HTMLInputElement).value;
  this.ycood = (document.getElementsByName('ycood')[0] as HTMLInputElement).value;
  this.addArray.push({
   xcood:this.xcood,
   ycood:this.ycood
  })
 }
}

在這里面,如果我們不定義

xcood: any;

ycood: any;

的話,那么將會出現(xiàn)如下錯誤

我們沒有聲明就直接初始化他們了,肯定會出錯,要記住一件事,要用到什么變量,首先要先聲明它,再去給它初始化。

在additem()函數(shù)里面,我們要初始化這兩個變量了,記住要用this,否則獲取不到全局作用域聲明的變量。因為我們是點擊添加按鈕再去獲取form表單里面的數(shù)據(jù),所以在邏輯上我們要把獲取的步驟放在additem()函數(shù)里面。這里還有一個新的寫法,因為之前我直接用

this.xcood = document.getElementsByName('xcood')[0].value;是獲取不到數(shù)據(jù)的,

所以我在網上找了一下,替換成了上面那種寫法。

我們在一開始就聲明了一個addArray的數(shù)組,這個數(shù)組即將存放的是一條一條的數(shù)據(jù)對象,在additem()函數(shù)里面每調用一次就把獲取到的數(shù)據(jù)push給這個數(shù)組。

接下來我們就要在子組件接收這個數(shù)組,并且渲染到表格上。

3.dataTable.component.html

<table class="table table-striped">
 <thead>
  <tr>
   <th>X坐標</th>
   <th>Y坐標</th>
   <th>操作</th>
  </tr>
 </thead>
 <tbody *ngIf="array.length!==0"><!--這里我們判斷一下傳遞過來的數(shù)組是否為空,如果是空的話我們就沒有必要渲染出來了-->
  <tr *ngFor="let data of array">
   <td>{{data.xcood}}</td>
   <td>{{data.ycood}}</td>
   <td><button type="button" class="btn btn-default" (click)="delete(data)">刪除</button></td>
  </tr>
 </tbody>
</table>

4.dataTable.component.ts

import { Component,Input } from '@angular/core';

@Component({
 selector: 'data-table',
 templateUrl: './dataTable.component.html',
 styleUrls: ['./dataTable.component.css']
})
export class DataTableComponent {
  @Input() array:any;//接收父組件傳遞過來的addArray數(shù)組
  index: number;   //跟上面說的一樣要先聲明
  delete(data){
    this.index = this.array.indexOf(data);
    if (this.index > -1) {
      this.array.splice(this.index, 1);//跟上面說的一樣在初始化的時候要用到this
      }
  }
}

我們接下來給刪除按鈕的函數(shù)delete()編寫邏輯,我們要的效果是點擊哪一條就刪除哪一條,所以我們要先獲取到你要刪除的這條數(shù)據(jù)對象,然后在父組件傳遞過來數(shù)組里面查找到這條數(shù)據(jù)對象的位置,再用splice()函數(shù)刪除。

5.app.module.ts

記得要在app.module.ts里面注冊你新建的dataTable組件

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';
import { DataTableComponent } from './dataTable/dataTable.component';

@NgModule({
 declarations: [
  AppComponent,
  DataTableComponent
 ],
 imports: [
  BrowserModule
 ],
 providers: [],
 bootstrap: [AppComponent]
})
export class AppModule { }

四、ngx-bootstrap的導入

其實很簡單,需要先在cmd輸入 cnpm install ngx-bootstrap --save在當前目錄下安裝該模塊

然后在項目最后的出口html文件里面加入

復制代碼 代碼如下:

最后直接可以在你編寫樣式的時候使用了。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Angular獲取手機驗證碼實現(xiàn)移動端登錄注冊功能

    Angular獲取手機驗證碼實現(xiàn)移動端登錄注冊功能

    最近在使用angular來做項目,功能要求實現(xiàn)一是點擊按鈕獲取驗證碼,二是點擊登錄驗證表單。之前用jquery來做項目很好做,使用angular怎么實現(xiàn)呢?其實實現(xiàn)代碼也很簡單的,下面通過實例代碼給大家介紹下,需要的朋友參考下吧
    2017-05-05
  • Angular多選、全選、批量選擇操作實例代碼

    Angular多選、全選、批量選擇操作實例代碼

    列表批量選擇是一個開發(fā)人員經常遇到的功能,列表批量選擇的實現(xiàn)方式很多,這篇文章主要介紹了Angular多選、全選、批量選擇實例代碼,有興趣的可以了解一下。
    2017-03-03
  • 基于AngularJS拖拽插件ngDraggable.js實現(xiàn)拖拽排序功能

    基于AngularJS拖拽插件ngDraggable.js實現(xiàn)拖拽排序功能

    ngDraggable.js是一款比較簡單實用的angularJS拖拽插件,借助于封裝好的一些自定義指令,能夠快速的進行一些拖拽應用開發(fā)。本文先從基本概念入手,給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友參考下吧
    2019-04-04
  • AngularJS實現(xiàn)使用路由切換視圖的方法

    AngularJS實現(xiàn)使用路由切換視圖的方法

    這篇文章主要介紹了AngularJS實現(xiàn)使用路由切換視圖的方法,結合學生信息管理系統(tǒng)為例分析了使用controllers.js控制器來切換視圖的具體步驟與相關操作技巧,需要的朋友可以參考下
    2017-01-01
  • Angular 4依賴注入學習教程之Injectable裝飾器(六)

    Angular 4依賴注入學習教程之Injectable裝飾器(六)

    這篇文章主要給大家介紹了關于Angular 4依賴注入之Injectable裝飾器的相關資料,文中介紹的非常詳細,對大家具有一定的參考學習價值,需要的朋友們下面來跟著小編一起學習學習吧。
    2017-06-06
  • 淺析Angular 實現(xiàn)一個repeat指令的方法

    淺析Angular 實現(xiàn)一個repeat指令的方法

    這篇文章主要介紹了Angular 實現(xiàn)一個repeat指令的方法,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-07-07
  • 深入解析Angular動態(tài)導入和懶加載實例

    深入解析Angular動態(tài)導入和懶加載實例

    這篇文章主要為大家深入解析了Angular動態(tài)導入和懶加載實例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-10-10
  • angularjs ocLazyLoad分步加載js文件實例

    angularjs ocLazyLoad分步加載js文件實例

    本篇文章主要介紹了angularjs ocLazyLoad分步加載js文件,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-01-01
  • angularjs封裝bootstrap時間插件datetimepicker

    angularjs封裝bootstrap時間插件datetimepicker

    這篇文章主要介紹了angularjs封裝bootstrap時間插件datetimepicker 的相關資料,需要的朋友可以參考下
    2016-06-06
  • AngularJS 監(jiān)聽變量變化的實現(xiàn)方法

    AngularJS 監(jiān)聽變量變化的實現(xiàn)方法

    今天小編就為大家分享一篇AngularJS 監(jiān)聽變量變化的實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10

最新評論

新化县| 蕲春县| 白朗县| 康乐县| 赤壁市| 特克斯县| 娱乐| 平邑县| 拜城县| 岱山县| 若羌县| 池州市| 神农架林区| 喀喇沁旗| 修水县| 华宁县| 新乐市| 永济市| 铁力市| 淮阳县| 曲水县| 沅陵县| 华蓥市| 师宗县| 大悟县| 象山县| 灌云县| 万年县| 开封县| 木里| 新宾| 密云县| 琼海市| 卢湾区| 日喀则市| 黔西| 清徐县| 柳江县| 怀来县| 揭东县| 肃宁县|