Components: MdTooltip not working in Angular-CLI Project

Created on 28 Dec 2016  路  19Comments  路  Source: angular/components

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.

Most helpful comment

used md-tooltip insted mdTooltip

All 19 comments

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

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?

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.

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?

used md-tooltip insted mdTooltip

<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

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

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

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

try update your version to 2.0.0-alpha.10

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

@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 Thanks. Yes, I discovered the very same thing. Mine is working now after using one of the themes.

@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?

@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?

@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 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';

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.

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._

Was this page helpful?
0 / 5 - 0 ratings

Related issues

3mp3ri0r picture 3mp3ri0r  路  3Comments

constantinlucian picture constantinlucian  路  3Comments

Miiekeee picture Miiekeee  路  3Comments

michaelb-01 picture michaelb-01  路  3Comments

dzrust picture dzrust  路  3Comments