Angular Chat Properties
activeStateEnabled
The UI component switches to the active state when users press down the primary mouse button. When this property is set to true, the CSS rules for the active state apply. You can change these rules to customize the component.
Use this property when you display the component on a platform whose guidelines include the active state change for UI components.
alerts
The code snippet below produces the following result:
- <dx-chat
- [alerts]="alerts"
- (onMessageEntered)="onMessageEntered($event)"
- ></dx-chat>
- import { DxChatTypes } from "devextreme-angular/ui/chat";
- // ...
- export class AppComponent {
- alerts: DxChatTypes.Alert[] = [];
- newAlert: DxChatTypes.Alert =
- {
- id: 1,
- message: "You have been disconnected"
- };
- onMessageEntered() {
- setTimeout(() => {
- this.alerts = [...this.alerts, newAlert];
- }, 1000);
- }
- }
dataSource
Chat works with collections of objects or string
values.
Depending on your data source, bind Chat to data as follows.
Data Array
Assign the array to the dataSource option.Read-Only Data in JSON Format
Set the dataSource property to the URL of a JSON file or service that returns JSON data.OData
Implement an ODataStore.Web API, PHP, MongoDB
Use one of the following extensions to enable the server to process data according to the protocol DevExtreme UI components use:Then, use the createStore method to configure access to the server on the client as shown below. This method is part of DevExtreme.AspNet.Data.
app.component.tsapp.component.htmlapp.module.ts- import { Component } from '@angular/core';
- import CustomStore from 'devextreme/data/custom_store';
- import { createStore } from 'devextreme-aspnet-data-nojquery';
- @Component({
- selector: 'app-root',
- templateUrl: './app.component.html',
- styleUrls: ['./app.component.css']
- })
- export class AppComponent {
- store: CustomStore;
- constructor() {
- let serviceUrl = "https://url/to/my/service";
- this.store = createStore({
- key: "ID",
- loadUrl: serviceUrl + "/GetAction",
- insertUrl: serviceUrl + "/InsertAction",
- updateUrl: serviceUrl + "/UpdateAction",
- deleteUrl: serviceUrl + "/DeleteAction"
- })
- }
- }
- <dx-chat ...
- [dataSource]="store">
- </dx-chat>
- import { BrowserModule } from '@angular/platform-browser';
- import { NgModule } from '@angular/core';
- import { AppComponent } from './app.component';
- import { DxChatModule } from 'devextreme-angular';
- @NgModule({
- declarations: [
- AppComponent
- ],
- imports: [
- BrowserModule,
- DxChatModule
- ],
- providers: [],
- bootstrap: [AppComponent]
- })
- export class AppModule { }
Any other data source
Implement a CustomStore.
Regardless of the data source on the input, the Chat always wraps it in the DataSource object. This object allows you to sort, filter, group, and perform other data shaping operations. To get its instance, call the getDataSource() method.
Review the following notes about data binding:
Do not specify the items property if you specified the dataSource, and vice versa.
Data field names cannot be equal to
this
and should not contain the following characters:.
,:
,[
, and]
.
- The stores are immutable. You cannot change their configurations at runtime. Instead, create a new store or DataSource and assign it to the dataSource property as shown in the following help topic: Two-Way Property Binding.
height
This property accepts a value of one of the following types:
Number
The height in pixels.String
A CSS-accepted measurement of height. For example,"55px"
,"20vh"
,"80%"
,"inherit"
.Function (deprecated since v21.2)
Refer to the W0017 warning description for information on how you can migrate to viewport units.
items
Use this property to render a new message and specify initial messages in Angular.
- <dx-chat
- [items]="messages"
- (onMessageEntered)="onMessageEntered($event)"
- ></dx-chat>
- import { DxChatTypes } from "devextreme-angular/ui/chat";
- // ...
- export class AppComponent {
- messages: DxChatTypes.Message[] = [
- {
- timestamp: Date.now(),
- author: secondUser,
- text: `Hello! I'm here to help you. How can I assist you today?`,
- }
- ];
- onMessageEntered({ message }) {
- this.messages = [...this.messages, message];
- }
- }
messageTemplate
Name | Type | Description |
---|---|---|
component | dxChat |
The Chat instance. |
message |
The message text. |
The message's container. It is an HTML Element or a jQuery Element when you use jQuery.
- <dx-chat
- messageTemplate="message"
- >
- <div *dxTemplate="let data of 'message'">
- {{data.message.id + " " + data.message.text}}
- </div>
- </dx-chat>
onDisposing
A function that is executed before the UI component is disposed of.
Name | Type | Description |
---|---|---|
element |
The UI component's container. It is an HTML Element or a jQuery Element when you use jQuery. |
|
component | Chat |
The UI component's instance. |
onInitialized
Name | Type | Description |
---|---|---|
element |
The UI component's container. It is an HTML Element or a jQuery Element when you use jQuery. |
|
component | Chat |
The UI component's instance. |
- <dx-chat ...
- (onInitialized)="saveInstance($event)">
- </dx-chat>
- import { Component } from "@angular/core";
- import Chat from "devextreme/ui/data_grid";
- // ...
- export class AppComponent {
- chatInstance: Chat;
- saveInstance (e) {
- this.chatInstance = e.component;
- }
- }
See Also
- Get a UI component Instance in Angular
onMessageEntered
Name | Type | Description |
---|---|---|
component | Chat |
The UI component's instance. |
element |
The UI component's container. It is an HTML Element or a jQuery Element when you use jQuery. |
|
event | Event (jQuery or EventObject) |
The event that caused the function to execute. It is an EventObject or a jQuery.Event when you use jQuery. |
message |
The entered message. |
Use this function to transfer messages to the backend:
- <dx-chat
- [items]="messages"
- (onMessageEntered)="onMessageEntered($event)"
- ></dx-chat>
- import { DxChatTypes } from "devextreme-angular/ui/chat";
- // ...
- export class AppComponent {
- messages: DxChatTypes.Message[] = [];
- onMessageEntered({ message }) {
- this.messages = [...this.messages, message];
- this.sendToBackend(message);
- }
- sendToBackend(message: DxChatTypes.Message) {
- console.log(`Message sent to backend: '${message.text}'`);
- // Backend logic goes here
- }
- }
onOptionChanged
Name | Type | Description |
---|---|---|
value | any |
The modified property's new value. |
previousValue | any |
The modified property's previous value. |
name |
The modified property if it belongs to the first level. Otherwise, the first-level property it is nested into. |
|
fullName |
The path to the modified property that includes all parent properties. |
|
element |
The UI component's container. It is an HTML Element or a jQuery Element when you use jQuery. |
|
component | Chat |
The UI component's instance. |
The following example shows how to subscribe to component property changes:
- <dx-chat ...
- (onOptionChanged)="handlePropertyChange($event)">
- </dx-chat>
- import { Component } from '@angular/core';
- @Component({
- selector: 'app-root',
- templateUrl: './app.component.html',
- styleUrls: ['./app.component.css']
- })
- export class AppComponent {
- // ...
- handlePropertyChange(e) {
- if(e.name === "changedProperty") {
- // handle the property change here
- }
- }
- }
- import { BrowserModule } from '@angular/platform-browser';
- import { NgModule } from '@angular/core';
- import { AppComponent } from './app.component';
- import { DxChatModule } from 'devextreme-angular';
- @NgModule({
- declarations: [
- AppComponent
- ],
- imports: [
- BrowserModule,
- DxChatModule
- ],
- providers: [ ],
- bootstrap: [AppComponent]
- })
- export class AppModule { }
onTypingEnd
Name | Type | Description |
---|---|---|
component | Chat |
The UI component's instance. |
element |
The UI component's container. It is an HTML Element or a jQuery Element when you use jQuery. |
|
user |
The user who entered the message. |
onTypingStart
Name | Type | Description |
---|---|---|
component | Chat |
The UI component's instance. |
element |
The UI component's container. It is an HTML Element or a jQuery Element when you use jQuery. |
|
event | Event (jQuery or EventObject) |
The event that caused the function to execute. It is an EventObject or a jQuery.Event when you use jQuery. |
user |
The user who started typing. |
reloadOnChange
Specifies whether the Chat UI component displays newly entered messages immediately. This property only applies if dataSource is used.
When you send a message in a Chat (press the "Send" button), the Chat triggers the store's insert method and adds the message to the store.
If reloadOnChange is enabled (default), the dataSource reloads: clears all items and calls the load method to update itself. Chat automatically listens to dataSource changes and updates the message feed with new messages.
Disable reloadOnChange to manage large numbers of messages, prevent additional load requests, and control message rendering timing. Handle the store's CRUD operations and render messages as your needs dictate.
rtlEnabled
When this property is set to true, the UI component text flows from right to left, and the layout of elements is reversed. To switch the entire application/site to the right-to-left representation, assign true to the rtlEnabled field of the object passed to the DevExpress.config(config) method.
- DevExpress.config({
- rtlEnabled: true
- });
typingUsers
The following image displays messages that appear when a single user or multiple users are typing:
- <dx-chat
- (onTypingStart)="onTypingStart($event)"
- (onTypingEnd)="onTypingEnd($event)"
- [typingUsers]="typingUsers"
- ></dx-chat>
- import { DxChatTypes } from "devextreme-angular/ui/chat";
- // ...
- export class AppComponent {
- typingUsers: DxChatTypes.User[] = [];
- user: DxChatTypes.User = [{
- name: 'User'
- }];
- onTypingStart() {
- this.typingUsers = this.user;
- }
- onTypingEnd() {
- this.typingUsers = [];
- }
- }
user
This property value, or its part, may be specified automatically:
If you do not specify this property, the component creates a user with default settings.
If you assign an object without an ID to this property, an ID is autogenerated.
width
This property accepts a value of one of the following types:
Number
The width in pixels.String
A CSS-accepted measurement of width. For example,"55px"
,"20vw"
,"80%"
,"auto"
,"inherit"
.Function (deprecated since v21.2)
Refer to the W0017 warning description for information on how you can migrate to viewport units.
If you have technical questions, please create a support ticket in the DevExpress Support Center.