0

I have a routerLink="adduser" on the navbar and it is also within users.component.ts in the button Add Users. If I click on the navbar it takes me to the form.component.html page, but if I click on adduser button that is within the users-component.ts file then nothing happens and I don't understand why. Can anyone explain to me where I went wrong. I'm still new to this.

app.component.html

<div class="navbar navbar-default navbar-fixed-top">
     <div class="container">
       <div class="navbar-header">
         <a href="../" class="navbar-brand">ngProject</a>
         <button class="navbar-toggle" type="button" data-toggle="collapse" data-target="#navbar-main">
           <span class="icon-bar"></span>
           <span class="icon-bar"></span>
           <span class="icon-bar"></span>
         </button>
       </div>
       <div class="navbar-collapse collapse" id="navbar-main">
         <ul class="nav navbar-nav">

           <li>
             <a routerLink="user">Users</a>
           </li>
           <li>
             <a routerLink="adduser">Add Users</a>
           </li>

         </ul>



       </div>
     </div>
   </div>

<div class="container">
<div class="content">
<router-outlet></router-outlet>
</div>
</div>

users.component.ts

import {Component} from '@angular/core';
import 'rxjs/add/operator/map';
import {Http} from '@angular/http';
import {UserService} from './users.service';

@Component({
selector: 'user',
template: `
<div class="container">
  <h1>Users</h1>
<h2><a routerLink="adduser"><button class="btn btn-primary">Add User</button></a></h2>
  <table class="table table-bordered">
    <thead>
      <tr>
        <th>Firstname</th>
        <th>Email</th>
        <th>Edit</th>
        <th>Delete</th>
      </tr>
    </thead>
    <tbody *ngFor="let user of _users">
      <tr>
        <td>{{user.name}}</td>
        <td>{{user.email}}</td>
        <td><i class="glyphicon glyphicon-edit"></i></td>
            <td><i class="glyphicon glyphicon-remove"></i></td>
      </tr>

    </tbody>
  </table>
</div>
`,
providers: [UserService]

})


export class UsersComponent{
  private _users;
constructor(private _userService:UserService){
this._userService.getUsers()
.subscribe(res => {
this._users = res;

})

}


}

app.router.ts

import {ModuleWithProviders} from '@angular/core'; import {Routes, RouterModule} from '@angular/router'; import {FormComponent} from './form/form.component'; import {UsersComponent} from './users.component';

export const router: Routes = [

  {path: 'user', component: UsersComponent}, {path: 'adduser', component: FormComponent}

];

export const routes: ModuleWithProviders = RouterModule.forRoot(router);
user6680
  • 79
  • 6
  • 34
  • 78

2 Answers2

0

It does not seem to be valid HTML to nest button under a tag.

See here.

This is most likely the reason, try removing the button and just use ordinary a tag and see if that works for you. Maybe the button click works but a does not get "clicked" in this case and nothing happens.

Community
  • 1
  • 1
Ilya Chernomordik
  • 27,817
  • 27
  • 121
  • 207
  • I removed the button and made it like this

    Add User

    , but same issue. Nothing happens onClick and no errors are being thrown on the console. It's the same routerLink for the navbar so I don't understand why it's not working within the component.
    – user6680 Nov 10 '16 at 18:59
  • Don't have other suggestions, sorry. It looks ok, maybe you got cached result back after the fix? But I guess you'd checked already. – Ilya Chernomordik Nov 10 '16 at 19:04
0

I figured it out. When I click the link inside of the page and not on the navbar, it's registering user/adduser instead of just adduser so I added this to the app.router.ts and now it opens the page. {path: 'user/adduser', component: FormComponent}

user6680
  • 79
  • 6
  • 34
  • 78