components icon indicating copy to clipboard operation
components copied to clipboard

MdTooltip not working in Angular-CLI Project

Open darrenbrett opened this issue 9 years ago • 19 comments

#I am trying to implement a tooltip using Angular 2's material in an Angular-CLI project. The documentation is a little unclear on exactly what's needed. All I need is a standard tooltip popup to show up.

My setup:

  • MacBook Air

  • OS X El Capitan (version 10.11.6)

  • Angular CLI Version: 1.0.0-beta.22-1

  • Node: 6.8.0

In my component HTML I have this:

<button md-raised-button mdTooltip="This is a tooltip!" mdTooltipPosition="below">Button</button>

In my app.module I have this (edited for brevity):

import { MaterialModule } from '@angular/material';

@NgModule({
    imports: [
        MaterialModule.forRoot(),
    ]

I also have material and hammer.js listed in my package.json (as hammer.js is apparently a dependency for the mdTooltip). Here's my package.json:

{
  "name": "abc",
  "version": "0.0.1",
  "license": "MIT",
  "angular-cli": {},
  "scripts": {
    "start": "ng serve",
    "lint": "tslint \"src/**/*.ts\"",
    "test": "ng test",
    "pree2e": "webdriver-manager update",
    "e2e": "protractor"
  },
  "private": true,
  "dependencies": {
    "@angular/common": "2.2.3",
    "@angular/compiler": "2.2.3",
    "@angular/core": "2.2.3",
    "@angular/forms": "2.2.3",
    "@angular/http": "2.2.3",
    "@angular/material": "^2.0.0-beta.1",
    "@angular/platform-browser": "2.2.3",
    "@angular/platform-browser-dynamic": "2.2.3",
    "@angular/router": "3.2.3",
    "@types/socket.io": "^1.4.27",
    "@types/socket.io-client": "^1.4.29",
    "angular-in-memory-web-api": "~0.1.15",
    "angular2-chartjs": "^0.1.6",
    "chart.js": "^2.4.0",
    "core-js": "^2.4.1",
    "hammerjs": "^2.0.8",
    "ng2-charts": "^1.4.1",
    "rxjs": "5.0.0-beta.12",
    "socket.io": "^1.7.2",
    "socket.io-client": "^1.7.2",
    "ts-helpers": "^1.1.1",
    "zone.js": "^0.6.23"
  },
  "devDependencies": {
    "@angular/compiler-cli": "2.2.3",
    "@types/hammerjs": "^2.0.34",
    "@types/jasmine": "2.5.38",
    "@types/node": "^6.0.42",
    "angular-cli": "1.0.0-beta.22-1",
    "codelyzer": "~2.0.0-beta.1",
    "jasmine-core": "2.5.2",
    "jasmine-spec-reporter": "2.5.0",
    "karma": "1.2.0",
    "karma-chrome-launcher": "^2.0.0",
    "karma-cli": "^1.0.1",
    "karma-jasmine": "^1.0.2",
    "karma-remap-istanbul": "^0.2.1",
    "protractor": "4.0.9",
    "ts-node": "1.2.1",
    "typescript": "~2.0.3",
    "typings": "^1.3.2",
    "webdriver-manager": "10.2.5"
  }
}

I'm unclear on what I need to also import into the component.ts file and what else, if anything, is needed to get this working. Here's what I have for the component file now:

import { MaterialModule } from '@angular/material';
import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-room',
  templateUrl: './room.component.html',
  styleUrls: ['./room.component.less']
})
export class RoomComponent implements OnInit {

  otherImg = 'app/img/avatar.svg';

  others = [
    { id: 1, name: 'John Smith' },
    { id: 2, name: 'Javier Sanchez' }
  ];

  chat(other) {
    console.log('Room chat function initiated...');
  }

  constructor() { }

  ngOnInit() {
  }

}

Right now I see a button in my component view. But no tooltip shows up when I hover over or click on the button. It shows the other material design elements (raised button styling, etc), but no tooltip.

darrenbrett avatar Dec 28 '16 18:12 darrenbrett

Can't reproduce. Does the module your RoomComponent is a part of import the MaterialModule?

npx avatar Dec 29 '16 14:12 npx

Yes, it does. I have this in my room.component.ts file:

import { MaterialModule } from '@angular/material';

Do I need to import the toolTip specifcally? If so, what does that import look like?

darrenbrett avatar Dec 29 '16 22:12 darrenbrett

That typescript import is unnecessary. The NgModule that contains RoomComponent needs MaterialModule added to its "imports". Unless that is the root module; in which case something else is wrong.

clydin avatar Dec 29 '16 22:12 clydin

I only added the import to the RoomComponent when I was asked about it. Initially I didn't have the import in the RoomComponent, because I didn't think it was necessary. Either way, it's not working. To answer your question, I have Material Module listed in my root-module imports, and that's where the RoomComponent is listed under declarations. And, as I mentioned before, I see the material "raised" styling added to the button, I just see no evidence of the tooltip functionality. Any ideas?

darrenbrett avatar Dec 29 '16 23:12 darrenbrett

used md-tooltip insted mdTooltip

Yunchitz avatar Dec 30 '16 11:12 Yunchitz

<button mdTooltip="some message" mdTooltipPosition="below">Button</button>

should be replaced by

<button md-tooltip="some message" md-tooltip-position="below">Button</button>

in the documentation: https://github.com/angular/material2/blob/master/src/lib/tooltip/README.md

Toub avatar Dec 31 '16 12:12 Toub

Both selectors should work in beta.1. The dashed selectors are deprecated and will be removed in an upcoming release.

clydin avatar Dec 31 '16 12:12 clydin

Either way, I tried both "mdTooltip" and "md-tooltip", and neither one worked.

darrenbrett avatar Jan 02 '17 04:01 darrenbrett

i also have some problems with tooltip. It doesn't disappear after mouseout.

Yunchitz avatar Jan 03 '17 12:01 Yunchitz

try update your version to 2.0.0-alpha.10

Yunchitz avatar Jan 03 '17 12:01 Yunchitz

I am having the same issue. Neither selector worked. Updating the version to 2.0.0-alpha.10 was also unsuccessful

elainaRenee avatar Jan 12 '17 22:01 elainaRenee

@darrenbrett If you are still struggling with this, I found that the tooltip and other angular material components will not work unless you use one of their themes or define a custom one. There is documentaion on how to implement a theme here: https://material.angular.io/guide/theming

elainaRenee avatar Jan 17 '17 16:01 elainaRenee

@elainaRenee Thanks. Yes, I discovered the very same thing. Mine is working now after using one of the themes.

darrenbrett avatar Jan 18 '17 06:01 darrenbrett

@elainaRenee Yes, with a theme it works. But now my colors look strange. Do you know the name of the default-theme that is taken when no theme is included?

alexandermikuta avatar Feb 22 '17 15:02 alexandermikuta

@elainaRenee , @darrenbrett Hi, I am facing a similar issue and I am new to angular material. Could you please explain how we should use the themes?

syedaFathima avatar Feb 27 '17 11:02 syedaFathima

@elainaRenee so basically what you are saying is mdTooltip only works with any one of the material themes.How could i get one working without these themes. any suggestions

rahul-krish avatar Dec 30 '17 05:12 rahul-krish

@rahul-krish As you can see in this code (for the tooltip theme), it requires a theme: https://github.com/angular/material2/blob/c3d7cd96de6f06e1530658e41d1e8aa3ba74805e/src/lib/tooltip/_tooltip-theme.scss#L11-L15 https://github.com/angular/material2/blob/c3d7cd96de6f06e1530658e41d1e8aa3ba74805e/src/lib/core/theming/_all-theme.scss#L36-L68 Therefore, there's no workaround for this. You have to have a theme.

@import '~@angular/material/prebuilt-themes/indigo-pink.css';

EdricChan03 avatar Dec 30 '17 05:12 EdricChan03

Also found out that matTooltip doesn't work properly on structural element such as *ngIf or *ngFor. Any matTooltip on or inside these element won't work.

forkadam avatar Feb 10 '18 16:02 forkadam

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.