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);
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