RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 927003
Accepted
A. Victor
A. Victor
Asked:2020-12-30 06:42:14 +0000 UTC2020-12-30 06:42:14 +0000 UTC 2020-12-30 06:42:14 +0000 UTC

为什么将事件包装在超时中会破坏事件?如何避免?

  • 772

Angular 7. 购物清单应用程序。带有用于排序的产品 div 和颜色按钮的列。例如,“土豆”和“洋葱”可以涂成相同的颜色,这样在使用时会并排放置。还有一个十字 - “购买”的标志。元素重新排列

css / diplay:flex / order:[вычисляемое].

生成的元素*ngFor

订单更改为[ngStyle]

一切正常。但是,当您单击排序时,元素立即消失的位置对用户来说并不明显。我决定在排序之前先计算好方向,然后{margin-top: -70px; }在50ms.

并且当前位置的替换应该推迟100ms。事件}点击子元素shop.component.html

传送到shop.component.ts

变成一个函数 d(elem: any, sort: number)

(自元素和新的排序号)

计算完方向后,元素被赋予了新的样式{margin-top: -70px;}

然后产生一个事件:“排序按钮被按下”,

传递给父组件app.component.ts,

从那里到

data.service.ts

并告诉模型对象新值{order:[]}

事件的到达速度超过 50 毫秒,这会导致即时重建,然后元素在新位置执行一点点舞蹈。这不是我想要的。如果您将事件的启动包装在一个计时器 EVEN ZERO 中:

setTimeout(function() {
              this.ColorBtnClicked.emit(elem);
       }, 0 );

该事件根本不会发出。现在,该元素的行为应如此:它以正确的方向模拟飞行的开始并重建,但它没有获得正确的颜色并且没有收到像 1)2) ... -8) 之类的前缀(这是data.service.ts的任务)。如果您删除超时,那么事件会立即完成它的工作(绘制 div 并放置前缀),然后元素会在一个新的地方跳舞,它在屏幕外是不可见的。

我将尝试以简洁的方式放置代码:

shop.component.html

 <div id="shopping" style="display: block;">
      <ul id="listingUL">
        <li *ngIf="!isFirst" class="item" [ngClass] = "{itemCrossedOut: item.done}" [ngStyle] ="{backgroundColor: 'var(--color' + ('' + item.order) + ')'}">

          <div class="main">
            <span class="itm">{{item.content}}</span>
            <button (click)="d(item, 9)" class="ok">X</button>
          </div>
          <div  class="orderDiv">
            <button (click)="d(item, 1)" class="orderBtn d1">1</button>
            <button (click)="d(item, 2)" class="orderBtn d2">2</button>
            <button (click)="d(item, 3)" class="orderBtn d3">3</button>
            <button (click)="d(item, 4)" class="orderBtn d4">4</button>
            <button (click)="d(item, 5)" class="orderBtn d5">5</button>
            <button (click)="d(item, 6)" class="orderBtn d6">6</button>
            <button (click)="d(item, 7)" class="orderBtn d7">7</button>
            <button (click)="d(item, 8)" class="orderBtn d8">8</button>
          </div>
        </li>
        </div>

      </ul>
    </div>

shop.component.ts

import {
  Component,
  Input,
  EventEmitter,
  Output,
  OnInit
} from '@angular/core';
import {Item} from '../item';

@Component({
  selector: 'app-shop-comp',
  templateUrl: './shop.component.html',
  styleUrls: ['./shop.component.css']
  })

export class ShopComponent{
  @Input() shopData: Item[];

  @Output() ColorBtnClicked = new EventEmitter<any>();


    d(elem: any, sort: number) {
      let newDone;
      let dir: number; // направление

      // здесь вычисляем новое направление

      if (dir > 0) {elem.direction = '70px 0px -70px 0px';}
      else  if (dir < 0) {elem.direction = '-70px 0px 70px 0px';}


      // setTimeout(function() {
        elem.done = newDone;
        if (sort !== 9) {elem.order = sort; }
        this.ColorBtnClicked.emit(elem);
      // }, 100 );


      setTimeout(function() {
        elem.direction = '0px 0px 0px 0px';
      }, 200 );
  }
}

app.component.ts

    import {Component, OnInit} from '@angular/core';
import {DataService} from './data.service';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss'],
  providers: [DataService]
})
export class AppComponent implements OnInit {

  constructor(private dataService: DataService) {}

  title = 'Shop-Reminder-App';
  activeScreen = 'inp';
  // activeAdv = 'slp';
  listing = '';
  shopData = [];

  onClickScreenBtn(screen) {
    this.activeScreen = screen;
  }

  onlistingAreaChanged(listing) { // слушаем событие изменился listing и отправляем текст в DataService
    this.shopData = this.dataService.rebuildData(listing);
  }

  ColorBtnClicked() { // слушаем нажатие на цвето-сортировку
        this.listing = this.dataService.rebuildText();
}

  setOldText(obj: any) {
    let text = '';
    for (let i = 0; i < obj.length; i++) {
         text = text + obj[i].content + ',';
    }
    this.shopData = this.dataService.rebuildData(text);
  }



  ngOnInit() {
    this.setOldText(this.shopData);
  }
}

数据服务.ts

import {Item} from './item';

export class DataService {
  private data: Item[] = [];

  // getData(): Item[] {
  //   return this.data;
  // }


  rebuildData(text) {

        this.data = [];
       // присваиваем прорядок согласно номеру 1)-8), если он есть

    return this.data;
  }

  rebuildText() { 
                 //Заглавие, номера 1)2)3)4)5)6)7)8) done(х)
    let text = '';
      // после нажатия на сортировку модифицируем список:  
      //Заглавие, номера 1)2)3)4)5)6)7)8) done(х)    
    return text;
  }
}

项目.ts

export class Item {
  constructor(
    public order: number,
    public content: string,
    public done: boolean,
    public direction: string,
  ) {}
}
angular2
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    user176262
    2020-12-30T06:50:55Z2020-12-30T06:50:55Z
    setTimeout((function() {
      this.ColorBtnClicked.emit(elem);
    }).bind(this), 0);
    

    或者

    setTimeout(() => this.ColorBtnClicked.emit(elem), 0);
    

    或者

    setTimeout(function(context) {
      context.ColorBtnClicked.emit(elem);
    }, 0, this);
    
    • 1

相关问题

  • 有什么方法可以加快 Angular2+ ssr 的速度吗?

  • 在独立的 Angular2 组件之间传递数据

  • Angular2 应用程序缩小。加载时间优化

  • 如何摆脱错误“类型‘订阅者[]’上不存在属性‘数据’”?角度 2

  • 根据角色删除页面元素?

  • 如何将多个订阅者订阅到一个观察者

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    是否可以在 C++ 中继承类 <---> 结构?

    • 2 个回答
  • Marko Smith

    这种神经网络架构适合文本分类吗?

    • 1 个回答
  • Marko Smith

    为什么分配的工作方式不同?

    • 3 个回答
  • Marko Smith

    控制台中的光标坐标

    • 1 个回答
  • Marko Smith

    如何在 C++ 中删除类的实例?

    • 4 个回答
  • Marko Smith

    点是否属于线段的问题

    • 2 个回答
  • Marko Smith

    json结构错误

    • 1 个回答
  • Marko Smith

    ServiceWorker 中的“获取”事件

    • 1 个回答
  • Marko Smith

    c ++控制台应用程序exe文件[重复]

    • 1 个回答
  • Marko Smith

    按多列从sql表中选择

    • 1 个回答
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Suvitruf - Andrei Apanasik 什么是空? 2020-08-21 01:48:09 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5