如何在Angular中使用Typescript初始化mdb tooltips?

huangapple go评论107阅读模式
英文:

How do I initialize mdb tooltips on Angular with Typescript?

问题

我正在使用Angular项目中的mdb,并且想要在我的密码输入上显示一个提示,向用户展示限制条件。

我尝试在HTML输入标签中添加data-mdb-toggle="tooltip"和紧随其后的title,但我发现mdb的提示需要初始化。我不知道如何在TypeScript中做到这一点,以下是我的.ts文件:

  1. import { Component, OnInit } from '@angular/core';
  2. import { FormControl, FormGroup, Validators } from '@angular/forms';
  3. @Component({
  4. selector: 'app-register',
  5. templateUrl: './register.component.html',
  6. styleUrls: ['./register.component.scss']
  7. })
  8. export class RegisterComponent implements OnInit {
  9. registerForm!: FormGroup;
  10. constructor() {}
  11. ngOnInit(): void {
  12. this.registerForm = new FormGroup({
  13. nome: new FormControl(null, Validators.required),
  14. cognome: new FormControl(null, Validators.required),
  15. team: new FormControl(null, Validators.required),
  16. genere: new FormControl(null, Validators.required),
  17. anno: new FormControl(null, [Validators.required, Validators.max(2005), Validators.min(1964)]),
  18. email: new FormControl(null, [Validators.required, Validators.email]),
  19. password: new FormControl(null, [Validators.required,Validators.pattern('(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&].{8,}')]),
  20. })
  21. }
  22. onSubmit() {
  23. console.log(this.registerForm);
  24. }
  25. }

如果需要进一步的帮助,请随时提出。

英文:

I'm using mdb on my angular project and I want to show a tooltip on my password input to show the restriction to the user.

I tried putting data-mdb-toggle="tooltip" and the title next to it in the html input tag but I saw that mdb tooltips have to be initialised. I don't now how to do it with Typescript, here's my .ts file:

  1. import { Component, OnInit } from '@angular/core';
  2. import { FormControl, FormGroup, Validators } from '@angular/forms';
  3. @Component({
  4. selector: 'app-register',
  5. templateUrl: './register.component.html',
  6. styleUrls: ['./register.component.scss']
  7. })
  8. export class RegisterComponent implements OnInit {
  9. registerForm!: FormGroup;
  10. constructor() {}
  11. ngOnInit(): void {
  12. this.registerForm = new FormGroup({
  13. nome: new FormControl(null, Validators.required),
  14. cognome: new FormControl(null, Validators.required),
  15. team: new FormControl(null, Validators.required),
  16. genere: new FormControl(null, Validators.required),
  17. anno: new FormControl(null, [Validators.required, Validators.max(2005), Validators.min(1964)]),
  18. email: new FormControl(null, [Validators.required, Validators.email]),
  19. password: new FormControl(null, [Validators.required,Validators.pattern('(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&].{8,}')]),
  20. })
  21. }
  22. onSubmit() {
  23. console.log(this.registerForm);
  24. }
  25. }

答案1

得分: 0

请尝试以下内容,希望这对您有用

在终端中轻松为您的项目安装 MDB ANGULAR UI KIT,请键入以下命令:

  1. npm i mdb-angular-ui-kit

然后,在 app.module.ts 中添加单独的 MDB 模块导入,以使用特定的组件。例如,使用工具提示:

  1. import { MdbTooltipModule } from 'mdb-angular-ui-kit/tooltip';
  2. @NgModule({
  3. ...
  4. imports: [MdbTooltipModule],
  5. ...
  6. })

然后在 HTML 模板中:

  1. <p class="mb-0">
  2. 鼠标悬停在链接上以查看
  3. <a href="#" mdbTooltip="Hi! I'm tooltip" #tooltip="mdbTooltip">tooltip</a>
  4. </p>
英文:

Please try below hope this will work for you

Install MDB ANGULAR UI KIT in your project easily type the following command in the terminal:

  1. npm i mdb-angular-ui-kit

then Add imports of individual MDB modules to the app.module.ts to use specific components. For example use tooltip:

  1. import { MdbTooltipModule } from &#39;mdb-angular-ui-kit/tooltip&#39;;
  2. @NgModule ({
  3. ...
  4. imports: [MdbTooltipModule],
  5. ...
  6. })

Then in html template

  1. &lt;p class=&quot;mb-0&quot;&gt;
  2. Hover the link to see the
  3. &lt;a href=&quot;#&quot; mdbTooltip=&quot;Hi! I&#39;m tooltip&quot; #tooltip=&quot;mdbTooltip&quot;&gt;tooltip&lt;/a&gt;
  4. &lt;/p&gt;

答案2

得分: 0

使用以下命令安装 UI 组件库:npm i mdb-angular-ui-kit

在您的 app.module.ts 文件中导入 MdbTooltipModule,如下所示:

  1. import { MdbTooltipModule } from 'mdb-angular-ui-kit/tooltip';
  2. @NgModule({
  3. ...
  4. imports: [
  5. MdbTooltipModule,
  6. ]
  7. ...
  8. })

确保在您的样式表中导入 mdbstylesheet

  1. // MDB SCSS
  2. @import "~mdb-angular-ui-kit/assets/scss/mdb.scss";

您的 HTML 模板代码如下:

  1. <a
  2. mdbTooltip="Tooltip on top"
  3. placement="top"
  4. >
  5. Tooltip on top
  6. </a>
英文:

Install ui kit using this command: npm i mdb-angular-ui-kit

Import MdbTooltipModule into your app.module.ts file like below:

  1. import { MdbTooltipModule } from &#39;mdb-angular-ui-kit/tooltip&#39;;
  2. @NgModule({
  3. ...
  4. imports: [
  5. MdbTooltipModule,
  6. ]
  7. ...
  8. })

Make sure you import mdbstylesheet in your stylesheet

  1. // MDB SCSS
  2. @import &quot;~mdb-angular-ui-kit/assets/scss/mdb.scss&quot;;

Your HTML template code like below:

  1. &lt;a
  2. mdbTooltip=&quot;Tooltip on top&quot;
  3. placement=&quot;top&quot;
  4. &gt;
  5. Tooltip on top
  6. &lt;/a&gt;

huangapple
  • 本文由 发表于 2023年1月9日 19:38:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75056739.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定