I have a button that I need to give permission to a specific user If he logs in into a website. I can do this with *ngIf on the HTML file but I would like to give the HTML element an id and then do the permission operation on the .ts file.
<ul class="navbar-nav float-right" *ngIf="user.name === 'John'" >
<li class="nav-item" >
<a class="nav-link" routerLink="Private">Private</a>
</li>
</ul>
Thanks in advance.
HTML
<ul class="navbar-nav float-right" id="ulPrivate">
<li class="nav-item" >
<a class="nav-link" routerLink="Private">Private</a>
</li>
</ul>
TS
const el: HTMLElement = document.getElementById('ulPrivate');
el.setAttribute("style", "color:red; border: 1px solid blue;");
Obviously change the style to "display:none" or "display:block" depending on the appropriate privacy condition: user.name === 'John'
References:
Declaring an HTMLElement Typescript
How to set multiple CSS style properties in typescript for an element?
You can use template ref syntax in your html file. And use ViewChild at your component code.
<ul class="navbar-nav float-right" #privateUL>
<li class="nav-item" >
<a class="nav-link" routerLink="Private">Private</a>
</li>
</ul>
--
#ViewChild("privateUL", { static: false }) ul: ElementRef;
ngAfterViewInit() {
// modify your element
this.ul.nativeElement.style.backgroundColor = "blue";
}
Check this stackblitz sample.
if you want add id to the element dynamically you can do it this way
<ul class="navbar-nav float-right" [attr.id]="'test'">
<li class="nav-item" >
<a class="nav-link" routerLink="Private">Private</a>
</li>
</ul>
But I will recommended to you usage of ngIf instead of id like this
<ul class="navbar-nav float-right" *ngIf="FunctionFromTs()">
<li class="nav-item" >
<a class="nav-link" routerLink="Private">Private</a>
</li>
</ul>
FunctionFromTs() which will return boolean status of visibility
Related
Can I make all Bootstrap classes in the main DIV instead of assigning them to each DIV or INPUT, for instance, this is the normal Bootstrap navbar, I'm assuming if possible to collect all the classes in the DIV, I've heard about it but didn't find it anywhere. Thanks
<div class="collapse navbar-collapse" id="Navbar">
<ul class="navbar-nav mr-auto">
<li class="nav-item"><a class="nav-link" href="index.html">Home</a></li>
<li class="nav-item active"><a class="nav-link" href="./aboutus.html">About</a></li>
<li class="nav-item"><a class="nav-link" href="#">Menu</a></li>
<li class="nav-item"><a class="nav-link" href="contactus.html">Contact</a></li>
</ul>
</div>
I'm not sure if you're asking specifically about bootstrap, i.e., whether or not those two classes can be used together or if you're just asking in general?
You can apply as many classes to an element as you want, just separate class names with space
<div class="active dropdown-toggle custom-class"></div>
but only a single id:
<div id = "btn1 btn"> <!-- this is wrong --> </div>
So I am a beginner in angular and I have to simply make a navbar.I have my bootstrap loaded in the .json file and it looks like this:-
"styles": [
"./node_modules/bootstrap/dist/css/bootstrap.min.css",
"src/styles.css"
],
whereas my code looks like this:-
<ul class=" nav navbar-nav navbar-right">
<ul class="dropdown-menu">
<li >
<a class="nav-link" href="#">Save Data</a>
</li>
<li >
<a class="nav-link" href="#">Fetch Data</a>
</li>
</ul>
</ul>
</div>
</div>
</nav>
`
Output is :-
my save data and fetch data are not appearing on right.what is the reason?
Just replace "navbar-right" with "ml-auto".
But still things are wrong with your code apart from "navbar-right".
It should be -
<ul class="navbar-nav ml-auto">
<li >
<a class="nav-link" href="#">Save Data</a>
</li>
<li >
<a class="nav-link" href="#">Fetch Data</a>
</li>
</ul>
I am familiar with the tutorial you are working with, so I guess you are creating a dropdown menu, this should be the complete code for it-
<ul class = "navbar-nav ml-auto">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" role="button" data-toggle="dropdown">
Manage
</a>
<div class="dropdown-menu">
<a class="dropdown-item" href="#">Save Data</a>
<a class="dropdown-item" href="#">Fetch Data</a>
</div>
</li>
</ul>
These existing questions might help -
Bootstrap NavBar with left, center or right aligned items
ml-auto is not pushing navbar links to the right
I'm working on a page in a Node Web app with a subnav that looks like this:
Here's the vanilla HTML for it:
<div class="col-md-8">
<ul class="nav nav-tabs">
<li class="nav-item">
<a class="nav-link active" href="#">Overview</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Comments</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
</li>
</ul>
</div>
Is there a way for me to create live links (e.g. Comments) on those tabs without having to render a whole separate route and EJS template? To me, a process like that would be more efficient and DRY than creating separate templates for each link in the subnav.
Any help would be greatly appreciated. Thanks!
I have tried the following code,
<ul class="nav nav-tabs">
<li class="nav-item" id="web1Tab"><a class="nav-link active" data-toggle="tab" id="w1" href="link_to_website_1">Website1</a></li>
<li class="nav-item" id="web2Tab"><a class="nav-link" data-toggle="tab" id="w2" href="link_to_website_2">Website2</a></li>
</ul>
It did look just like as I expected, however href doesn't work. On the other hand, if I remove the data-toggle="tab" part then href's work properly but active tab doesn't change. I guess that, href in this case only supports inner linking? I am not sure. The code looks very simple but I was unable solve it.
Can you try surrounding your entire list item with a href?
<ul class="nav nav-tabs">
<a style="display:block" href="your_url1_here">
<li class="nav-item" id="web1Tab">Website1</li>
</a>
<a style="display:block" href="your_url2_here">
<li class="nav-item" id="web2Tab">Website2</li>
</a>
</ul>
I have a navbar component with this code:
<ul class="navbar-nav mx-auto">
<li class="nav-item active">
<a class="nav-link" href="#" >Inicio <span class="sr-only"></span></a>
</li>
<li class="nav-item active">
<a class="nav-link" href="#">Here Link Home Section 2 <span class="sr-only"></span></a>
</li>
</ul>
and have another component HomeComponent with 3 sections:
<section id="home_one">
stuff
section
one
</section>
<section id="home_two">
stuff
section
two
</section>
<section id="home_three">
stuff
section
three
</section>
How can I link from navbar links to specific section (section 2) in another component (homeComponent)?
Angular 6.1 has new featured called anchorScrolling, Enable this option and use Router fragement
app.module.ts
#NgModule({
imports: [BrowserModule, FormsModule, RouterModule.forRoot(routes, {
anchorScrolling: 'enabled',
})],
declarations: [AppComponent, HelloComponent, ComponentbComponent],
bootstrap: [AppComponent]
})
app.component.html
<a [routerLink]="['item']" [fragment]="'section2'">
section2
</a>
<router-outlet></router-outlet>
Example:https://stackblitz.com/edit/angular-zgmsuw
In an angular way Its better for you to use Angular Router
Create Three components Instead of Sections
home_one
home_two
home_three
Then In your App Module Or in Seperate Routing Module declare you routes like:
const routes: Routes = [
{ path: 'home_one', component: home_one},
{ path: 'home_two', component: home_two },
{ path: 'home_three', component: home_three }
{ path: '', component: home }
{ path: '*', component: home }
];
Then In your Imports add routes
#NgModule({
imports: [RouterModule.forRoot(routes)],
})
Then In you 'Home page' or where ever you want to Render these sections Add Router Outlet, It will be something like this
<your-navbar-component></your-navbar-component>
<router-outlet></router-outlet>
In Your navbar configure your Anchor for your routes properly
<ul class="navbar-nav mx-auto">
<li class="nav-item " routerLinkActive="active" [routerLinkActiveOptions]="{exact: true}">
<a class="nav-link" [routerLink]="['/home_one']" >Home One <span class="sr-only"></span></a>
</li>
<li class="nav-item " routerLinkActive="active" [routerLinkActiveOptions]="{exact: true}">
<a class="nav-link" [routerLink]="['/home_two']" >Home Two<span class="sr-only"></span></a>
</li>
<li class="nav-item " routerLinkActive="active" [routerLinkActiveOptions]="{exact: true}">
<a class="nav-link" [routerLink]="['/home_three']" >Home Three<span class="sr-only"></span></a>
</li>
</ul>
For more Visit Official documentation:
https://angular.io/guide/router
If this is onclick of tabs and changing of corresponding section of tabs:-
.ts
tab = 'tab0';
.html
<ul class="navbar-nav mx-auto">
<li class="nav-item active" (click)='tab = "tab0"'>
<a class="nav-link" href="#" >Inicio <span class="sr-only"></span></a>
</li>
<li class="nav-item active" (click)='tab = "tab1"'>
<a class="nav-link" href="#">Here Link Home Section 2 <span class="sr-only"></span></a>
</li>
</ul>
<section id="home_one" *ngIf="tab ==='tab0'">
stuff
section
one
// Here you can pass selctor of a separate component for each section
// <tab0-component></tab0-component>
</section>
<section id="home_two" *ngIf="tab ==='tab1'">
stuff
section
two
</section>
<section id="home_three" *ngIf="tab ==='tab2'">
stuff
section
three
</section>
If you want to go to corresponding fragment onclick ie to scrollview in that document:-
Angular2 Routing with Hashtag to page anchor
If you want to change routes for each tab then:-
app.routing.ts
// create routes for each tab
.ts
constructor(public router:Router){
}
opentab(routes){
this.router.navigate[routes]
}
.html
<ul class="navbar-nav mx-auto">
<li class="nav-item active" (click)= 'opentab("tab0")'>
<a class="nav-link" href="#" >Inicio <span class="sr-only"></span></a>
</li>
<li class="nav-item active" (click)= 'opentab("tab1")'>
<a class="nav-link" href="#">Here Link Home Section 2 <span class="sr-only"></span></a>
</li>
</ul>
<router-outlet></router-outlet>