Angular 6 link to section in other component? - html

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>

Related

COREUI sidebar minimizer and dropdown is not working

I have to customize sidebar that is provided by COREUI 4.x theme. Below is the code for my sidebar
<div class="app-body">
<div class="sidebar">
<nav class="sidebar-nav">
<ul class="nav">
<li class="nav-item">
<a
class="nav-link"
routerLink="/dashboard"
[routerLinkActive]="['active']"
[routerLinkActiveOptions]="{ exact: true }"
>
<i class="icon-speedometer"></i> Dashboard
</a>
</li>
<li class="nav-item">
<a
class="nav-link"
href="#"
routerLink="/alerts-configuration"
[routerLinkActive]="['active']"
[routerLinkActiveOptions]="{ exact: true }"
>
<i class="icon-layers"></i> Alert Configuration
</a>
</li>
<li
class="nav-item nav-dropdown"
dropdown
[routerLinkActive]="['active']"
[routerLinkActiveOptions]="{ exact: true }"
>
<a class="nav-link nav-dropdown-toggle">
<i class="icon-puzzle"></i>Administration
</a>
<ul class="nav-dropdown-items">
<li class="nav-item">
<a
class="nav-link"
routerLink="/device-management/devices"
[routerLinkActive]="['active']"
[routerLinkActiveOptions]="{ exact: true }"
>
<i class=""></i>Devices Management
</a>
</li>
<li class="nav-item">
<a
class="nav-link"
routerLink="/sites"
[routerLinkActive]="['active']"
[routerLinkActiveOptions]="{ exact: true }"
>
<i class="icon-settings"></i>Sites Management
</a>
</li>
</ul>
</li>
</ul>
</nav>
<button class="sidebar-minimizer brand-minimizer" type="button"></button>
</div>
</div>
Now when I try to click on Administration, it wont open the dropdown and also when I try to minimize from the arrow on the bottom, this wont work as well. Please help!
I was having this issue also. I figured out that if the JavaScript files that control sidebar animation are added before the page is generated the sidebar doesn't work.
The solution:
Add the CORE UI JavaScript files at the end.
Looks a little like this:
<html>
<body>
{the entire html page}
<script src="https://coreui.io/demo/free/3.4.0/js/main.js" async defer></script>
</body>
</html>
"async defer" may or may not be necessary.
With coreui 4.x and BS 5 you should add data-coreui="navigation" to the sidebar-nav element and it will start working correctly.
Please find below my code that is working.
<li class="nav-item nav-dropdown " [appIsAuthorized]="roleconst.ProductAdminAbove" (click)="status=!status"
[ngClass]="status ? 'open' : ''">
<a class="nav-link nav-dropdown-toggle ">
<i class="nav-icon icon-puzzle"></i> Management
</a>

navbar - right not working in my html file

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

Navigatng between html pages is not working in Flask

Firstly I am very new to Flask. I created a templates folder and moved all the html files there. Now i created a static folder and moved all my js files, images and css files there. Now in my index.html, I have navigation links as follows,
<div class="collapse navbar-collapse tm-nav" id="navbar-nav">
<ul class="navbar-nav ml-auto">
<li class="nav-item active">
<a class="nav-link tm-nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link tm-nav-link" href="/services.html">Services</a>
</li>
<li class="nav-item">
<a class="nav-link tm-nav-link" href="/about.html">About</a>
</li>
</ul>
</div>
Now in my app.py for page navigation I am doing something like below. When I open http://127.0.0.1:5000/ in browser, I can only see the index.html. But when I click on services or about it says "404 Not found. The requested URL was not found on the server." Please help what am I doing wrong.
from flask import Flask, render_template, Response, request, redirect, url_for
app = Flask(__name__)
#app.route("/")
def index():
return render_template('index.html')
#app.route("/services")
def services():
return render_template('services.html')
#app.route("/about")
def about():
return render_template('about.html')
I see the following in the command prompt.
You need to read about routing in flask here
You need to call the function attached to the URL with url_for or you can also put the direct URL there. For example /services.
<div class="collapse navbar-collapse tm-nav" id="navbar-nav">
<ul class="navbar-nav ml-auto">
<li class="nav-item active">
<a class="nav-link tm-nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link tm-nav-link" href="{{url_for('services')}}">Services</a>
</li>
<li class="nav-item">
<a class="nav-link tm-nav-link" href="{{url_for('about')}}">About</a>
</li>
</ul>
</div>

Open other tab from sub-navbar without loading entire new HTML page

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!

How to give access to HTML element based on user name?

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