Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use material2 toolbar, button and angular-cli router

I have the following files:

.html

<nav>
  <md-toolbar color = "primary">
    <a [routerLink] = "['new-patient']">New Patient</a>

    <button md-icon-button
            color = "accent">
      <md-icon class = "material-icons md-24">person_add</md-icon>
    </button>
  </md-toolbar>
</nav>

.ts

import { Component, OnInit } from '@angular/core';
import { ROUTER_DIRECTIVES, ROUTER_PROVIDERS } from '@angular/router';
import { MdToolbar } from '@angular2-material/toolbar';
import { MdIcon, MdIconRegistry } from '@angular2-material/icon';
import { MdButton } from '@angular2-material/button';

@Component({
  moduleId: module.id,
  selector: 'epimss-toolbar',
  templateUrl: 'toolbar.component.html',
  styleUrls: ['toolbar.component.css'],
  providers: [MdIconRegistry],
 directives: [MdButton, MdIcon, MdToolbar, ROUTER_DIRECTIVES],
})
export class ToolbarComponent implements OnInit {

  constructor() {}

  ngOnInit() {
  }
}

My router actually works with the above code.

However, I am trying the
<a [routerLink] = "['new-patient']">New Patient</a>

to be a route that is activated when the

<button md-icon-button
        color = "accent">
  <md-icon class = "material-icons md-24">person_add</md-icon>
</button>

is clicked.

I have tried the following but it does not work.

<button md-icon-button
        color = "accent"
        [routerLink] = "['new-patient']">
  <md-icon class = "material-icons md-24">person_add</md-icon>
</button>

Appreciate any help given please, and thanks.

like image 351
st_clair_clarke Avatar asked May 27 '16 04:05

st_clair_clarke


1 Answers

Your issue is because the new Router doesn't accept [routerLink] on <button> elements, only <a> tags.

But you're in luck, Material lets you do the icon on <a> and <button>

Material Docs on md-button

So try this:

<a md-icon-button
        [routerLink] = "['new-patient']"
        color = "accent">
  <md-icon class = "material-icons md-24">person_add</md-icon>
</a>
like image 98
Dennis Smolek Avatar answered Oct 22 '22 07:10

Dennis Smolek