【Angular】angular范例写法汇总

14 篇文章 0 订阅
14 篇文章 1 订阅

1. *ngIf与else

<ng-template pTemplate="body" let-rowData let-expanded="expanded" let-columns="columns">
    <tr [pSelectableRow]="rowData">
        <td style="width: 3em">
            <p-tableCheckbox [value]="rowData"></p-tableCheckbox>
        </td>
        <ng-container *ngFor="let col of columns">
            <td [ngStyle]="col.style">
                <div *ngIf="col.field === 'sxId' then thenBlock else elseBlock"></div>
                <ng-template #thenBlock>
                    <button *ngIf="rowData[col.field] == 1" class="table-button table-button-1">现行有效</button>
                    <button *ngIf="rowData[col.field] == 2" class="table-button table-button-2">失效</button>
                    <button *ngIf="rowData[col.field] == 3" class="table-button table-button-3">已被修改</button>
                    <button *ngIf="rowData[col.field] == 5" class="table-button table-button-5">部分失效</button>
                </ng-template>
                <ng-template #elseBlock>{{rowData[col.field]}}</ng-template>
            </td>
        </ng-container>
        <td style="width: 4em">
            <a [pRowToggler]="rowData">
                详情
            </a>
        </td>
    </tr>
</ng-template>

2. angular图片加载路径

  1. Angular中,html中的路径要写成开头不带/的绝对路径。比如
<img src="assets/hello.png">
  1. Angular中,CSS中的路径要写成相对路径,比如
background-image: url('../../assets/hhh.png');
  1. 示例
<div class="ui-g-12">
   <ng-container *ngIf="accordFlag; else elseBlock">
     <img src="assets/images/form/accord.png" alt="鉴定一致" width="200">
   </ng-container>
   <ng-template #elseBlock>
     <img src="assets/images/form/disaccord.png" alt="鉴定一致" width="200">
   </ng-template>
</div>

3. tabView与router路由双向选中

页签可以对应路由?index=0

<p-tabView [(activeIndex)]="activeIndex" (onChange)="onTaskListTabChange($event)">
    <p-tabPanel [header]="tasklist.taskListName +  tasklist.total"
                [leftIcon]="tasklist.displayIcon"
                *ngFor="let tasklist of tasklists; let i = index"
                [selected]="i == activeIndex">
    </p-tabPanel>
</p-tabView>
activeIndex = 0; // 当前tab页
ngOnInit() {
	this.activeIndex = +this.route.queryParams['value']['index'] || 0;
}

/** 栏目切换 */
onTaskListTabChange(event) {
    let tasklist = this.tasklists[event.index];
    this.router.navigate([], { queryParams: { index: event.index }}); //更改路由为选中tab下标
}

4. 蚂蚁zorro的nz-table点击选中行添加样式

点击表格某一行,给指定的行添加选中样式
html

<tbody>
   <tr class="editable-row"
       *ngFor="let data of editRowTable.data;let i = index;"
       (click)="rowClick(data,i)"
       [ngClass]="{'msg-table-row-selected': selectedMsgIndex === i}">
     <td>{{ data.id }}</td>
     <td>{{ data.isOpen }}</td>
     <td>{{ data.messageLocation }}</td>
     <td>{{ data.messageStyle }}</td>
     <td>
       <div class="editable-cell" *ngIf="editId !== data.id; else editTpl">
         <div class="editable-cell-value-wrap" (click)="startEdit(data.id, $event)">
           {{ data.messageTemplate }}
         </div>
       </div>
       <ng-template #editTpl>
         <input type="text" pInputText [(ngModel)]="data.messageTemplate" />
       </ng-template>
     </td>
     <td>{{ data.messageLink }}</td>
     <td>{{ data.messageSql }}</td>
     <!-- <td>
       <a nz-popconfirm nzTitle="Sure to delete?" (nzOnConfirm)="deleteRow(data.id)">Delete</a>
     </td> -->
   </tr>
 </tbody>

js

/**
   * @description: 行选中
   * @param {*} data 选中行的数据
   * @param {*} i 选中行的下标
   * @return {*}
   */
  rowClick(data,i){
    this.selectedMsgIndex = i;
    this.taskList = this.taskAllList[i];
    this.buildForm();
  }

css

::ng-deep .msg-table-row-selected{
  background: #e6f7ff !important;
}

持续更新中…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

卸载引擎

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值