angular - routerLink only working from navbar -
i have routerlink="adduser" on navbar , within users.component.ts in button add users. if click on navbar takes me form.component.html page, if click on adduser button within users-component.ts file nothing happens , don't understand why. can explain me went wrong. i'm still new 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} '@angular/core'; import 'rxjs/add/operator/map'; import {http} '@angular/http'; import {userservice} './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} '@angular/core'; import {routes, routermodule} '@angular/router'; import {formcomponent} './form/form.component'; import {userscomponent} './users.component'; export const router: routes = [ {path: 'user', component: userscomponent}, {path: 'adduser', component: formcomponent} ]; export const routes: modulewithproviders = routermodule.forroot(router);
it not seem valid html nest button
under a
tag.
see here.
this reason, try removing button , use ordinary a
tag , see if works you. maybe button click works a
not "clicked" in case , nothing happens.
Comments
Post a Comment