How to insert array object property value into HTML? - html

I wanted to make a simple carousel using DOM manipulation, I don't know if I can do this or not in Angular, but the idea is I created an array of objects with properties that I want to pass to the HTML using interpolation, and I don't know how to do it yet. Here's my code:
<div class="container">
<div class="row">
<div class="col-12 text-center">
<h1 class="mt-5 mb-1"><strong>carousel</strong></h1>
<div class="container">
<div class="row">
<div class="col-12">
<div class="carousel-container">
<div class="carousel-cell">
<div class="row text-center">
<div class="col-12">
<div class="row carousel-control">
<div class="col-2 carousel-prev fa-2x" (click)="carouselPrev()">
<fa-icon [icon]="faChevronLeft" class="mr-2"></fa-icon>
</div>
<div class="col-7 col-md-2">
<div class="carousel-image">
<img [src]="carousel.image" alt="carousel image">
</div>
</div>
<div class="col-2 carousel-next fa-2x" (click)="carouselNext()">
<fa-icon [icon]="faChevronRight" class="mr-2"></fa-icon>
</div>
</div>
</div>
<div class="col-12">
<div class="carousel-text mt-4">
<h3>{{carousel.description}}</h3>
<p><em> {{carousel.name}}, <span>{{carousel.affiliation}}</span></em></p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
import { Component } from '#angular/core';
import { Router } from '#angular/router';
import {faChevronLeft, faChevronRight, IconDefinition} from '#fortawesome/free-solid-svg-icons';
#Component({
selector: 'app-carousel',
templateUrl: './carousel.component.html',
styleUrls: ['./carousel.component.scss']
})
export class CarouselComponent implements OnInit {
faChevronLeft: IconDefinition = faChevronLeft;
faChevronRight: IconDefinition = faChevronRight;
carousel = [
{
id: 1,
name: 'captain america',
affiliation: 'camp lehigh',
description: 'language!',
image: 'http://picsum.photos/200'
},
{
id: 2,
name: 'thor',
affiliation: 'asgard',
description: 'i am god of thunder!',
image: 'http://picsum.photos/200'
},
{
id: 3,
name: 'tony stark',
affiliation: 'stark industries',
description: 'and i....am..iron man!',
image: 'http://picsum.photos/200'
}
];
constructor(
) {}
ngOnInit() {
}
carouselPrev() {
// i want to make this a control by making the id/index +1
}
carouselNext() {
// i want to make this a control by making the id/index -1
}
}

You can achieve it using ngFor with index.
Live example: https://stackblitz.com/edit/angular-b5qvwy
Some code:
Basic HTML
<h1>{{list[i].name}}</h1>
<div (click)="prev()"><</div>
<div (click)="next()">></div>
Basic TS code
export class AppComponent {
list = [
{
name: "Jhon"
},
{
name: "Joe"
},
{
name: "Marth"
}
];
i = 0;
prev(){
if(this.i > 0){
this.i--;
}
}
next(){
if(this.i < this.list.length -1){
this.i++;
}
}
}
For your example, simply do that in the html:
<div class="container">
<div class="row">
<div class="col-12 text-center">
<h1 class="mt-5 mb-1"><strong>carousel</strong></h1>
<div class="container">
<div class="row">
<div class="col-12">
<div class="carousel-container">
<div class="carousel-cell" *ngIf="carousel[i].id">
<div class="row text-center">
<div class="col-12">
<div class="row carousel-control">
<div class="col-2 carousel-prev fa-2x" (click)="carouselPrev()">
<fa-icon [icon]="faChevronLeft" class="mr-2"></fa-icon>
</div>
<div class="col-7 col-md-2">
<div class="carousel-image">
<img [src]="carousel[i].image" alt="carousel image">
</div>
</div>
<div class="col-2 carousel-next fa-2x" (click)="carouselNext()">
<fa-icon [icon]="faChevronRight" class="mr-2"></fa-icon>
</div>
</div>
</div>
<div class="col-12">
<div class="carousel-text mt-4">
<h3>{{carousel[i].description}}</h3>
<p><em> {{carousel[i].name}}, <span>{{carousel[i].affiliation}}</span></em></p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>

Related

ERROR TypeError: Cannot read properties of undefined (reading 'quantity') - Angular 12

I have been facing this problem for quite some time now. When I try to add an item to my cart I get this error : ERROR TypeError: Cannot read properties of undefined (reading 'quantity').
Here is my cart service:
import { Injectable } from '#angular/core';
import { environment } from 'src/environments/environment';
import { Cart } from '../models/cart';
import { Item } from '../models/item';
import {Product} from '../models/product';
import {Subject} from 'rxjs';
#Injectable({
providedIn: 'root'
})
export class CartService {
cart:Cart=new Cart();
tva=environment.tva/100;
cart$=new Subject<Cart>();
constructor() { }
emitCart(){
this.cart$.next(this.cart)
}
addToCart(product:Product){
const item=this.cart.items.find(item=>item.product._id === product._id);
if(item){
item.quantity++;
}else{
const item=new Item();
item.product=product;
item.quantity=1;
this.cart.items.push(item);
}
this.updateCart();
}
updateCart(){
this.cart.items.forEach(item=>{
this.cart.resume.quantity+=item.quantity;
this.cart.resume.costHT+=item.quantity*item.product.price;
this.cart.resume.costTaxe+=this.cart.resume.costHT*this.tva;
this.cart.resume.costTTC+=this.cart.resume.costHT*(1+this.tva);
})
this.emitCart();
}
removeOne(product:Product){
}
removeMany(product:Product){
}
}
This is my header where a number should be added next to the shopping bag icon when I click on the add to cart icon :
This is my header component logic :
import { Component, OnInit } from '#angular/core';
import {AuthService} from "../../../services/auth.service";
import {CartService} from '../../../services/cart.service';
import {Cart} from '../../../models/cart';
#Component({
selector: 'node-header',
templateUrl: './header.component.html',
styleUrls: ['./header.component.css']
})
export class HeaderComponent implements OnInit {
isAuth;
resume:{quantity:number,costHT:number, costTaxe,costTTC:number};
constructor(private authService:AuthService,private cartService:CartService) { }
ngOnInit(): void {
this.cartService.cart$.subscribe(
(cart:Cart)=>{
this.resume=cart.resume;
console.log(cart.resume);
},
(err)=>{
console.log(err.message);
}
)
this.authService.isAuth$.subscribe(
(bool:boolean)=>{
this.isAuth=bool
}
)
}
logout(){
this.authService.logout();
}
}
And finally this is the html template of the header just in case :
<!-- header start -->
<header>
<!-- header top start -->
<div class="header-top theme1 bg-dark py-15">
<div class="container">
<div class="row align-items-center">
<div class="col-lg-6 col-md-7 order-last order-md-first">
<div class="static-info text-center text-md-left">
<p class="text-white">Join our showroom and get <span class="theme-color">10 % off</span> ! Coupon code : <span class="theme-color">Jstore2021</span></p>
</div>
</div>
<div class="col-lg-6 col-md-5">
<nav class="navbar-top pb-2 pb-md-0 position-relative">
<ul class="d-flex justify-content-center justify-content-md-end align-items-center">
<li>
Setting <i class="ion ion-ios-arrow-down"></i>
<ul class="topnav-submenu dropdown-menu" aria-labelledby="dropdown1">
<li>My account</li>
<li>Checkout</li>
<li>Sign out</li>
</ul>
</li>
<li>
USD $ <i class="ion ion-ios-arrow-down"></i>
</li>
<li class="english">
<a href="#" id="dropdown3" class="pr-0" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<img src="assets/img/logo/us-flag.jpg" alt="us flag"> English
<i class="ion ion-ios-arrow-down"></i>
</a>
</li>
</ul>
</nav>
</div>
</div>
</div>
</div>
<!-- header top end -->
<!-- header-middle satrt -->
<div class="header-middle pt-20">
<div class="container">
<div class="row align-items-center">
<div class="col-sm-6 col-lg-2 order-first">
<div class="logo text-center text-sm-left mb-30 mb-sm-0">
<img src="assets/img/logo/logo-dark.jpg" alt="logo">
</div>
</div>
<div class="col-sm-6 col-lg-5 col-xl-4">
<!-- search-form end -->
<div class="d-flex align-items-center justify-content-center justify-content-sm-end">
<div class="media static-media mr-50 d-none d-lg-flex">
<img class="mr-3 align-self-center" src="assets/img/icon/1.png" alt="icon">
<div class="media-body">
<div class="phone">
<span class="text-muted">Call us:</span>
</div>
<div class="phone">
(+33)752152560
</div>
</div>
</div>
<!-- static-media end -->
<div class="cart-block-links theme1">
<ul class="d-flex">
<li class="mr-0 cart-block position-relative" >
<a class="offcanvas-toggle" routerLink="/cart">
<span class="position-relative">
<i class="icon-bag"></i>
<span class="badge cbdg1" *ngIf="resume">{{resume.quantity}}</span>
</span>
<span class="cart-total position-relative" *ngIf="resume">{{resume.costTTC/100|currency:'EUR'}}</span>
</a>
</li>
<!-- cart block end -->
</ul>
</div>
<div class="mobile-menu-toggle theme1 d-lg-none">
<a href="#offcanvas-mobile-menu" class="offcanvas-toggle">
<svg viewbox="0 0 800 600">
<path
d="M300,220 C300,220 520,220 540,220 C740,220 640,540 520,420 C440,340 300,200 300,200"
id="top"></path>
<path d="M300,320 L540,320" id="middle"></path>
<path
d="M300,210 C300,210 520,210 540,210 C740,210 640,530 520,410 C440,330 300,190 300,190"
id="bottom" transform="translate(480, 320) scale(1, -1) translate(-480, -318)">
</path>
</svg>
</a>
</div>
</div>
</div>
<div class="col-lg-5 col-xl-6 order-lg-first">
<div class="search-form pt-30 pt-lg-0">
</div>
</div>
</div>
</div>
</div>
<!-- header-middle end -->
<!-- header bottom start -->
<!-- header bottom start -->
<nav class="header-bottom theme1 d-none d-lg-block">
<div class="container">
<div class="row align-items-center">
<div class="col-lg-10 offset-lg-2 d-flex flex-wrap align-items-center position-relative">
<ul class="main-menu d-flex">
<li routerLinkActive="active"><a routerLink="/">Home</a></li>
<li routerLinkActive="active"><a routerLink="/shop">Shop</a></li>
<li routerLinkActive="active"><a routerLink="/add-product">Add product</a></li>
<li routerLinkActive="active" ><a routerLink="/signin" *ngIf="!isAuth">Login</a></li>
<li routerLinkActive="active" ><a routerLink="/signup" *ngIf="!isAuth">Register</a></li>
<li routerLinkActive="active" style="color: black;cursor: pointer;" *ngIf="isAuth" (click)="logout()"><a >Logout</a></li>
</ul>
</div>
</div>
</div>
</nav>
<!-- header bottom end -->
</header>
Sorry I forgot to post the cart class and template so here they are :
import {Item} from '../models/item';
export class Cart {
items:Item[]=[];
resume:{quantity:number,costHT:number, costTaxe:number,costTTC:number};
}
And the item class :
import { Product } from "./product";
export class Item {
product:Product;
quantity:number;
}
All I needed to do was add this line as the first line of the updateCart() function:
resume={quantity=0,costHT=0, costTaxe=0,costTTC=0};

how to make the components in same height in vue

I have a ticket component in vue:
<template>
<div class="col-lg-3 col-md-6 col-sm-12 col-xs-12 single-ticket-col">
<div class="single-ticket">
<div type="ticket" class="widget --flex-column">
<div class="top --flex-column">
<div class="ticket-type -bold">{{ product.attributes.name }}</div>
<!-- TODO: Change image url to product's image-->
<img :src="image" alt="Product Image" />
<div class="deetz --flex-row-j!sb">
<div class="event --flex-column">
<div class="date">{{ formatDate(product.attributes.date) }}</div>
<div class="price">€ {{ priceCalculationCheapest(product) }} - € {{ priceCalculationExpensive(product) }} <span>(Inkl. Mwst.)</span></div>
<div v-for="variation in product.attributes.variations">
<div class="variation">
{{variation.name}}
<div v-if="variation.scales.length != 0">
<div v-for="scale in variation.scales">
{{scale.from_amount}} for € {{scale.price_incl_vat}}
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="rip"></div>
<div class="bottom --flex-row-j!sb">
<router-link :to="{name: 'products', params: {id: product.id}}" class="btn button">Auswählen</router-link>
</div>
</div>
</div>
</div>
</template>
And some tickets have extra information, that's why they are not in the same height sometimes as you can see in the picture:
As a main div of the component I defined as all-tickets and I wrote my css like this:
.all-tickets {
display: flex;
width: 100%;
}
.all-tickets .single-ticket-col {
flex: 1;
}
But I am not successful. Could you please help me in this topic how can i make them in the same height?

how to properly append after the first object and before the button object?

I am trying to add a card horizontally. But it appending after the button, not in the first card.
HTML code and jquery:
<div class="flex-box-tabs"></div>
<div class="tab-panels">
<div id="panel1" class="panel active">
<div class="flex-box-panel">
<div class="flex-box-card">
<div class="card">
<h1>Budget</h1>
<div class="card-body">
<h5 class="card-title">Month 1</h5>
Add Daliy Expenses
</div>
</div>
<div class="card">
<div class="pos">
<button class="round-button">+</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script>
$(function(){
$('button').on('click', function() {
$('<div class="card"><h1>Budget</h1><div class="card-body"><h5 class="card-title">Month 1</h5>Add Daliy Expenses</div> </div>').appendTo('.flex-box-card');
})
})
</script>
Is there some other way to append it between the first card and button?
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<div class="flex-box-tabs"></div>
<div class="tab-panels">
<div id="panel1" class="panel active">
<div class="flex-box-panel">
<div class="flex-box-card">
<div class="card">
<h1>Budget</h1>
<div class="card-body">
<h5 class="card-title">Month 1</h5>
Add Daliy Expenses
</div>
</div>
<div class="card" id="button">
<div class="pos">
<button class="round-button">+</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script>
$(function(){
$('button').on('click', function() {
$('#button').before('<div class="card"><h1>Budget</h1><div class="card-body"><h5 class="card-title">Month 1</h5>Add Daliy Expenses</div> </div>')
})
})
</script>
use before() method to add an element before the selected element.
If you want to append the .card to bottom but before the button you can use .insertBefore('.card:last'); And If you need to append it to top use prependTo()
$(function(){
$('button').on('click', function() {
$('<div class="card"><h1>Budget</h1><div class="card-body"><h5 class="card-title">Month 1</h5>Add Daliy Expenses</div> </div>').insertBefore('.card:last');
})
})
.card{
margin : 10px;
padding : 10px;
background : #eee;
display : inline-block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="flex-box-tabs"></div>
<div class="tab-panels">
<div id="panel1" class="panel active">
<div class="flex-box-panel">
<div class="flex-box-card">
<div class="card">
<h1>Budget</h1>
<div class="card-body">
<h5 class="card-title">Month 1</h5>
Add Daliy Expenses
</div>
</div>
<div class="card">
<div class="pos">
<button class="round-button">+</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>

ngIf user is logged in show component

I'm making an app that needs to be accessible for users without an account but as soon as someone makes an account they need to be able to see additional functionality and it needs to be responsive as well.
For responsiveness I'm using material design for bootstrap and angular materialmdbootstrap. In the example bellow I'd want to change the mdb-col-size from 9 to 12 depending on if the user is logged in or not and thus not display save,... option(s).
<div>
<div class="row ">
<div class="col-lg-9 col-md-8 col-sm-6 ">
<div class="row ">
<div class="col-md-12 mb-12">
<div class="card testimonial-card ">
<div class="card-up lighten-1 ">
<div class="card-body">
<h4 class="card-title">{{ dare.title }}</h4>
<hr />
<p>{{ dare.description }}</p>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="col-lg-3 col-md-4 col-sm-6"><!--this shouldn't be displayed when someone is not logged in-->
<div class="row">
<div class="col-12">
<button mat-raised-button class="btn btn-block">
Save
</button>
</div>
</div>
<br />
<div class="row">
<div class="col-12">
<button mat-raised-button class="btn btn-block">
Challenge someone else
</button>
</div>
</div>
<br />
<div class="row">
<div class="col-12">
<button mat-raised-button class="btn btn-block">
Create new ...
</button>
</div>
</div>
</div>
</div>
<br />
</div>
I expect to see the options only when someone is logged in and the responsiveness changing accordingly.
Thanks you in advance!
You should rather use Guards in your project, example in documentation: https://angular.io/api/router/CanActivate
To achieve it you should implement two components (one for authenticated user, another for non-authenticated user). Define routing, for example:
{
path: '',
component: AuthenticatedUserComponent,
canActivate: [AuthenticatedUserGuard]
},
{
path: '',
component: UnauthenticatedUserComponent
}
And then define guard:
#Injectable()
class AuthenticatedUserGuard implements CanActivate {
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean|UrlTree>|Promise<boolean|UrlTree>|boolean|UrlTree {
return true; // here you should call a method from service, that
// that holds whether user is authenticated or not
}
}

Pages not loading in Single Page Application Angular JS

I am trying to implement Single Page Application in AngularJs. However, when I select the link ({{item.Name}} in Tree.Html). Corresponding view is not displayed in ng-View.
Any help will be appreciated
Main.html
<body ng-app="InfoModule" ng-controller="MainController" style="max-width:1000px; margin:auto">
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">Information</a>
</div>
<span class="pull-right navbar-text">{{UserName}}</span>
</div>
</nav>
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
Info Page
<div ui-tree data-drag-enabled="false">
<ol ui-tree-nodes ng-model="itemList" class="font-weight-normal">
<li ng-repeat="item in itemList track by $index" ui-tree-node ng-include="'Tree.html'">
</li>
</ol>
</div>
</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<div ng-include="Details"></div>
<div data-ng-view>
</div>
</div>
</div>
<div class="panel-footer">
(C) My Solutions
</div>
Tree.html
<div ui-tree-handle class="tree-node tree-node-content">
<a class="btn btn-success btn-xs" ng-if="item.nodes && item.nodes.length > 0" ng-click="toggle(this)">
<span class="glyphicon" ng-class="{'glyphicon-chevron-right': collapsed,'glyphicon-chevron-down': !collapsed}"></span>
</a>
<a href="#Details" >
{{item.Name}}
</a>
Master.js
var app = angular.module('InfoModule', ['ui.tree', 'ngRoute', 'ngStorage']);
app.config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider) {
$routeProvider
.when('/login', {
templateURL: 'Login.html',
controller: '/mYscriptS/LoginController.js'
})
.when('Details', {
templateURL: 'pages/Details.html',
controller: '../mYscriptS/DetailsController.js'
})
.when('/Main', {
templateURL: 'main.html',
controller: '/mYscriptS/MainController.js'
});
//.otherwise({
// redirectTo: 'pages/Main.html'
// //templateURL: '/Main.html',
// //controller: '/mYscriptS/MainController.js'
//});
$locationProvider.html5Mode({
enabled: true,
requireBase: false
});
$locationProvider.html5Mode(true);
}]);
Details.html
<div ng-controller="DetailsController" >
<div class="row">
<div class="col-md-6">
User Id
{{UserName}}
</div>
<div class="col-md-6">
<input type="text" name="txtUserId" />
</div>
</div>
<div class="row">
<div class="col-md-6">
Password
</div>
<div class="col-md-6">
<input type="text" name="txtPassword" />
</div>
</div>
Once I had gotten a problem that was same as yours.
I've tried to figure it out, and many people recommended using 'ui.router' instead of 'ngRoute'.
Maybe there are more differences between them specifically.
But even just this explain, could help you deciding side.
ngRoute is a angular core module which is good for basic scenarios. I believe that they will add more powerful features in upcoming releases.
Ui-router is a contributed module which is overcome the problems of ngRoute. Mainly Nested/Complex views.
URL: https://github.com/angular-ui/ui-router