简体   繁体   English

如何从视图中的数组中删除项目

[英]How to delete item from an array in a view

I am building my own notification component in my Angular5 application. 我正在Angular5应用程序中构建自己的通知组件。 I have an array of strings. 我有一个字符串数组。

messages: Message[] = new Array();
this.messages[0]= new Message("Toast 1");
this.messages[1]= new Message("Toast 2");
this.messages[2]= new Message("Toast 3");

and in view I'm using a loop to iterate through this array: 并且鉴于我正在使用循环来迭代此数组:

  <aside  *ngFor="let item of messages">
    <span  class="notification" style="position:absolute">
     {{item.Text}}
    </span>
  </aside>

And generally this mechanism works fine. 通常,此机制运行良好。 I use the CSS to hide every message after 3 seconds. 我使用CSS在3秒后隐藏了每条消息。

But the problem I faced is that, the elements in loop are not deleted from the array. 但是我面临的问题是,循环中的元素不会从数组中删除。 This cause that elements hide and show again and again. 这导致元素隐藏并一次又一次显示。 I traied to set for ex. 我准备出发。 item.IsDeleted = true, but this seems to be wrong, because error 'Bindings cannot contain assignments' is still being thrown. item.IsDeleted = true,但这似乎是错误的,因为仍然抛出错误“绑定不能包含分配”。

I'm looking for a solutin, to workaround this. 我正在寻找一种solutin,以解决此问题。 Is there any chance to add bindings/assigments in view? 有没有机会在视野中添加绑定/辅助? Maybe there is other way to achive the same? 也许还有其他方法可以达到同样的效果?

EDIT 编辑

I don't want to use a database. 我不想使用数据库。 I created a compoment and a service. 我创建了一个工具和服务。 In every component which need to use notifications, the service is injected in a contructor, and then new messages are passed via this service to NotificationComponent. 在需要使用通知的每个组件中,将服务注入到构造器中,然后新消息通过此服务传递到NotificationComponent。

I don't think you can simply add an element to an empty array like this: 我认为您不能像这样简单地将元素添加到空数组中:

myArray[n] = someVariable;

you should use 你应该使用

myArray.push(someVariable); 

and after the array actually has something inside you can use 在数组实际上有东西之后,您可以使用

myArray[n]

in order to access an element... I havn't tested this but I'm pretty sure your syntax would result in assigning a "new Message("blah blah")" to an undefined var so not actually in your array... Feel free to correct me but I'm pretty sure that's your problem there... as someone else said, instead, to remove an element you use: 为了访问元素...我没有测试过,但是我很确定您的语法会导致为未定义的var分配“ new Message(“ blah blah”)“,因此实际上不在您的数组中。 。随时纠正我,但我很确定那是您的问题...就像其他人所说的那样,删除您使用的元素:

myArray.splice(i, 1);

splice takes two parameters: index of the object you want to remove and the number of element you want to remove... your array is automatically updated since it's actually an instance of an object you are operating onto.. and that method (splice) returns the array of deleted items... so for example: splice有两个参数:要删除的对象的索引和要删除的元素的数目...数组自动更新,因为它实际上是您要操作的对象的实例..以及该方法(splice)返回已删除项目的数组...因此,例如:

let myArray: Number[] = [];
//equal to your
//let myArray = new Array();
//but with more common syntax and declaring the type before to avoid post-compile errors

myArray.push(1);
myArray.push(2);
myArray.push(3);
myArray.push(4);
myArray.push(5);
//at this point myArray is [1, 2, 3, 4, 5]

let x = 2;
let result = myArray.splice(x, 1);
let numberAtXPosition = result[0];
//result -> [3]
//numberAtXPosition = 3;

UPDATE UPDATE

Ok i now tested if you can add elements your way: You can... (tho I'm pretty sure using push() is considered cleaner/prettier by pretty much everybody) 好的,现在我测试了是否可以按自己的方式添加元素:您可以...(我很确定使用push()被几乎所有人认为更干净/更漂亮)

But anyway simply inside your component you should add a method 但是无论如何,只要在您的组件内部添加一个方法

public post(message: String) {
    this.messages.push(message);
    setTimeout(() => {

        this.messages.splice(this.messages.indexOf(message, 1));

    }, 3 * 1000);
}

this will automatically set a timer to remove the newly entered element from your array after 3 seconds... no need to hide from css or anything... angular will automatically bind your array to your element with *ngFor 这将自动设置一个计时器,在3秒后从您的数组中删除新输入的元素...无需从CSS或其他任何东西中隐藏... angular将使用* ngFor将数组自动绑定到您的元素

Since you are apparently new to angular i suggest you implement that notification service as.. well.. a service... available throughout all of you app... Read the angular documentation on services (or a tutorial) and just create a NotificationService that you can inject in all your components... my ideal suggestion is a NotificationService such as: 因为您显然对angular不熟悉,所以我建议您将通知服务实现为....服务...在您所有应用程序中都可用...阅读有关服务的angular文档(或教程),然后仅创建NotificationService您可以注入所有组件...我的理想建议是NotificationService,例如:

//INSIDE NotificationService class
public subject: Subject<String> = new Subject<String>();
public post(message: String) {
    this.subject.next(message);
}

//INSIDE your NotificationComponent
constructor(private notifications: NotificationService) {}

ngOnInit() {
    this.notifications.subject.subscribe((next) -> {
        this.post(next);
    })
}

public post(message: String) {
    this.messages.push(message);
    setTimeout(() => {

        this.messages.splice(this.messages.indexOf(message, 1));

    }, 3 * 1000);
}

this way you will be able to send a notification from any component... for example, let's imagine the following is a component that handles another part of your interface 这样,您将能够从任何组件发送通知...例如,让我们想象以下是处理界面另一部分的组件

//INSIDE ANOTHER COMPONENT
constructor(private notifications: NotificationService) {}

foo() {
    let x: Number;
    ...
    ... some logic
    ...
    ... need to send a notification?
    ...
    this.notifications.post('Ehy my computation results were ' + x);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM