Angular 2 news ticker
A news ticker in Angular 2! Here's a basic example to get you started:
Components
news-ticker.component.ts
:import { Component, OnInit } from '@angular/core';
@Component({ selector: 'app-news-ticker', template: `
- {{ news.title }}
constructor() {}
ngOnInit(): void { // You can add a timer to update the news items every X seconds setInterval(() => { this.newsItems.push({ title: 'New News Item!' }); this.newsItems.shift(); // Remove the oldest item }, 5000); // Update every 5 seconds } }
2. `app.component.ts`:
```typescript
import { Component } from '@angular/core';
import { NewsTickerComponent } from './news-ticker/news-ticker.component';
@Component({
selector: 'app-root',
template: `
<app-news-ticker></app-news-ticker>
`,
})
export class AppComponent {}
CSS
Add the following CSS to your styles.css
file to style the news ticker:
.news-ticker {
position: relative;
width: 300px;
height: 30px;
overflow: hidden;
background-color: #f0f0f0;
padding: 10px;
border: 1px solid #ccc;
}
.news-ticker ul {
list-style: none;
margin: 0;
padding: 0;
}
.news-ticker li {
display: inline-block;
margin-right: 10px;
font-size: 16px;
font-weight: bold;
color: #333;
}
.news-ticker li:last-child {
margin-right: 0;
}
Usage
In your Angular 2 application, add the NewsTickerComponent
to your app component:
<app-root>
<app-news-ticker></app-news-ticker>
</app-root>
This will render a news ticker component with a list of news items. The newsItems
array is updated every 5 seconds using setInterval
, and the oldest item is removed to keep the list updated.
You can customize the appearance and behavior of the news ticker by modifying the CSS and the NewsTickerComponent
code.