Hello guys,
I have a problem with back slash in background-image style in Vuejs and string replace in HTML not work and solution.
URL from inspecting element
http://localhost:8000/storage/banar-pages\July2018\WPNrFE6eXopKnjMqjNgW.jpg
From DB
banar-pages\July2018\WPNrFE6eXopKnjMqjNgW.jpg
Code from component file
<div class="block-entry fixed-background" :style="'background-image: url(' +link + '/storage/' + about.image +');'">
<div class="container">
<div class="row">
<div class="col-sm-6 col-sm-offset-3">
<div class="cell-view simple-banner-height text-center">
<div class="empty-space col-xs-b35 col-sm-b70"></div>
<h1 class="h1 light">{{ about.name }}</h1>
<div class="title-underline center"><span></span></div>
<div class="simple-article light transparent size-4">{{ about.details }}</div>
<div class="empty-space col-xs-b35 col-sm-b70"></div>
</div>
</div>
</div>
</div>
</div>
Thank you
I need to replace image link it but flage not work image.replace('/\\/g', '/')
It looks like you're incorrectly passing a string as the first argument to String#replace(), which would result in a literal replacement (i.e., it would replace the first occurrence of /\/g in the string):
console.log('XX/\\/gXX/\\/gXX'.replace('/\\/g', '/'))
Remove the quotes from the first argument to make it a regular expression:
console.log('\\path\\to\\foo.png'.replace(/\\/g, '/'))
Then, your Vue template could be similar to this:
<div :style="'background-image: url(' +link.replace(/\\/g, '/') + '/storage/' + about.image.replace(/\\/g, '/') +');'">
new Vue({
el: '#app',
data() {
return {
link: 'http:\\placekitten.com',
about: {
image: '\\100\\100'
}
}
}
})
.dummy {
width: 100px;
height: 100px;
}
<script src="https://unpkg.com/vue#2.5.16"></script>
<div id="app">
<div class="dummy"
:style="`background-image: url(${link.replace(/\\/g, '/')}${about.image.replace(/\\/g, '/')})`">
</div>
</div>
You could have a method for processing that string and returning a revised URL:
methods: {
prepareURL(string) {
return string.replace('/\\/g', '/');
}
}
Component code:
<div class="block-entry fixed-background" :style="'background-image: url(' +link + '/storage/' + prepareURL(about.image) +');'">
<div class="container">
<div class="row">
<div class="col-sm-6 col-sm-offset-3">
<div class="cell-view simple-banner-height text-center">
<div class="empty-space col-xs-b35 col-sm-b70"></div>
<h1 class="h1 light">{{ about.name }}</h1>
<div class="title-underline center"><span></span></div>
<div class="simple-article light transparent size-4">{{ about.details }}</div>
<div class="empty-space col-xs-b35 col-sm-b70"></div>
</div>
</div>
</div>
</div>
Related
I have multiple divs with id='title'.
When that div is hovered over I want a hidden div with id="source" to appear.
I am using the following code but only the first div with id='title' on the page works.
<!-- Show Source / Date on mouse hover -->
<script>
$(document).ready(function(){
$("#title").mouseenter(function(){
$("#source").fadeIn( 200 );
});
$("#title").mouseleave(function(){
$("#source").fadeOut( 200 );
});
});
</script>
HTML Example:
<div id="title" class="col-md-3">
<div id="source" style="display:none">Hidden Text</div>
</div>
<div id="title" class="col-md-3">
<div id="source" style="display:none">Hidden Text</div>
</div>
<div id="title" class="col-md-3">
<div id="source" style="display:none">Hidden Text</div>
</div>
Use classes instead of IDs
use .find() to search for descendants of an element
jQuery(function($) { // DOM ready and $ alias in scope
$(".title").on({
mouseenter() {
$(this).find(".source").fadeIn(200);
},
mouseleave() {
$(this).find(".source").fadeOut(200);
}
});
});
.title {
display: flex;
}
/* Utility classe */
.u-none {
display: none;
}
<div class="title col-md-3">
TITLE 1
<div class="source u-none">Visible Text</div>
</div>
<div class="title col-md-3">
TITLE 2
<div class="source u-none">Hidden Text</div>
</div>
<div class="title col-md-3">
TITLE 3
<div class="source u-none">Hidden Text</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
Hello Δce Let's start by modifying the source code to use classes for you to pick. This helps us describe the functionality and allow the ID's to serve their purpose.
we'll take your
<div class="col-md-3 show-source-on-hover">
<div class="source" style="display:none">Hidden Text</div>
</div>
<div class="col-md-3 show-source-on-hover">
<div class="source" style="display:none">Hidden Text</div>
</div>
<div class="col-md-3 show-source-on-hover">
<div class="source" style="display:none">Hidden Text</div>
</div>
and we can update the jQuery code
<script>
$(function() {
$(".show-source-on-hover")
.mouseenter(function() {
$(this).find('.source').fadeIn( 200 );
})
.mouseleave(function() {
$(this).find('.source').fadeOut( 200 );
});
});
</script>
You can see here there's the use of this wrapped in the $() jquery object function. and the new use of .find to get get the correct source class.
For a working demo please see the following link: https://codepen.io/jessycormier/pen/GRmaaEb
Note: your use of ID's should change and always try and be unique like others have suggested. MDN (Mozilla Developer Network) has some great documentation on the standards of HTML, CSS, and JavaScript which can help give you.
Also, in the demo I've given your column divs some default size otherwise there is nothing to trigger the mouse over events. Good luck and happy coding 🚀
IDs are IDENTIFIERS. What that means is that there can only be one per document. Classes are not unique, so you can use them.
✅ $(".title")
❌ $("#title")
I have a problem loading a component inside another component. It just doesn't show , but in browser the route changes , but my component doesn't update...
So i have:
--homepage
--coins--
--coinspreview <-- i cannot access this component;
--services
app-component.html
<app-homepage>
</app-homepage>
app-homepage.html
<section id="home">
<div class="bg">
<img class="my-bg" src="/assets/images/bg.jpg">
</div>
<div class="container middle">
<div class="row">
<div class="col-lg-12 col-sm-12 col-12">
<div class="hero">
<h4 class="hero-title">
Top 100 Cryptocurrency
</h4>
</div>
</div>
</div>
</div>
</section>
<section id="coins">
<app-coins></app-coins>
//here it shows my another component
</section>
app-coins.html
<div class="container">
<div class="row justify-content-center align-items-center">
<div class="col-lg-3 col-md-4 col-sm-6 col-6" *ngFor="let coin of coins$ | async">
<div class="main" [routerLink]="['./coins/coins-preview']">
<img class="mini" src="{{coin.image}}" alt="{{coin.name}}" />
<div class="name">{{coin.name}}</div>
<div class="prices">
<p class="coin-price">
Pret actual: {{coin.current_price}}
</p>
<p class="coin-market-cap">
Capitalizare: {{coin.market_cap}}
</p>
</div>
</div>
</div>
</div>
</div>
And this are all my routes inside app-routing-module.
const routes: Routes = [
{
path: '',
redirectTo: 'homepage',
pathMatch: 'full'
},
{
path: 'homepage',
component: HomepageComponent
},
{
path: "coins",
component: CoinsComponent,
children: [
{
path: "coins/coins-preview",
component: CoinsPreviewComponent
}
]
}
];
I don't know what to do else.. Im so stuck but I cannot understand why It doesnt load ????
When defining the child route dont repeat the parent route name. That should make it working
my landing page component declares in it a header component like so
<div class="container-fluid">
<div class="intro-section">
<div class="background">
//some stuff
</div>
<div class="content">
<app-page-header></app-page-header>
<div class="row">
<div class="col-12 text header mt-3">
Hello World
</div>
<div class="col-12 text-center mt-4 mb-4">
<button class="btn know-more-btn text" (click)="scroll(subscriptions)">Tell me More</button>
</div>
</div>
</div>
</div>
<div #subscriptions></div>
<div id="others" class="otherSection">
//other stuff
</div>
</div>
the scroll function in landing-page.component.ts is
scroll(el: HTMLElement) {
el.scrollIntoView({behavior:"smooth"});
}
I've also tried this
scrolling() {
(document.getElementById('others')).scrollIntoView({behavior:"smooth"});
}
I want to have a button in my app-page-header component that triggers or perform the same action an "scroll(subscriptions)" i.e in the landing page scroll to subscriptions. How can I achieve this?
Create an EventEmitter inside you app-page-header like
#Output() onButtonClicked: EventEmitter<any> = new EventEmitter();
The button in your app-page-header will emit the action to the parent component like:
buttonClicked() {
onButtonClicked.emit();
}
On your landing page add this to call the scroll function whenever the event is emitted.
<app-page-header (onButtonClicked)="scroll(subscriptions)"></app-page-header>
I have rows of data and each row has 2 columns.
<div class="row">
<div class="col-md-2"><strong>Natural ID:</strong></div>
<div class="col-md-4">{{ctrl.firstobject.naturalId}}</div>
<div class="col-md-2"><strong>ManulaID:</strong></div>
<div class="col-md-4">{{ctrl.firstobject.manualID}}</div>
</div>
Now, I would like to compare the value of the field called "NaturalID" with another Natural ID (accessed by ctrl.CustomObject.naturalid) and if these two values are different, I would like to highlight in red the field corresponding to Natural ID in the above HTML. How can I do this?
You can use ng-class something like
var myApp = angular.module('myApp',[]);
myApp.controller('MyCtrl',function($scope, $timeout) {
$scope.firstobject={
naturalId:1,
manualID:2
}
$scope.customObj={
naturalId:2
}
});
.red{
color:red;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl" >
<div class="row">
<div class="col-md-2"><strong>Natural ID:</strong>
</div>
<div class="col-md-4" ng-class="{'red':firstobject.naturalId !=customObj.naturalId}">{{firstobject.naturalId}}<span ng-if="firstobject.naturalId !=customObj.naturalId" class="glyphicon glyphicon-alert"></span>
</div>
<div class="col-md-2"><strong>ManulaID:</strong>
</div>
<div class="col-md-4">{{firstobject.manualID}}</div>
</div>
</div>
here is my index.html
<html ng-app="dempApp">
<head>
<!-- CSS -->
</head>
<body>
<div ng-switch on="templateHeader">
<div ng-switch-when="login">
<div loginheader></div>
</div>
<div ng-switch-when="home">
<div homeheader></div>
</div>
</div>
</body>
<!-- JS scripts -->
</html>
My app.js has the directives
demoClientApp.directive('loginheader', function () {
return {
restrict: 'A',
replace: true,
templateUrl:'views/loginheader.html'
}
})
demoClientApp.directive('homeheader', function () {
return {
restrict: 'A',
replace: true,
templateUrl:'views/homeheader.html'
}
})
loginheader.html is like following
<div class="main-container">
<div class="main-content">
<div class="row">
<div class="col-sm-10 col-sm-offset-1">
<div class="login-container">
<div class="center">
<h1>
<span class="blue">DemoApplication</span>
</h1>
</div>
<div class="position-relative">
<div ng-view></div>
</div><!-- /position-relative -->
</div>
</div><!-- /.col -->
</div><!-- /.row -->
</div>
</div><!-- /.main-container -->
It has ng-view inside and similarly for homeheader directive I have another html file which also has ng-view.
Based on path, I am setting value of templateHeader and its switching the directive. But the page is blank now and shows error.
Error: Template must have exactly one root element. was: <div class="main-container">
<div class="main-container">
<div class="main-content">
<div class="row">
<div class="col-sm-10 col-sm-offset-1">
<div class="login-container">
<div class="center">
<h1>
<span class="blue">DemoApplication</span>
</h1>
</div>
<div class="position-relative">
<div ng-view></div>
</div><!-- /position-relative -->
</div>
</div><!-- /.col -->
</div><!-- /.row -->
</div>
</div><!-- /.main-container -->
If I don't use the loginheader directive and use the html directly, it is working fine and the other homeheader directive is working. Why is that?
How to solve it?
UPDATE 1
It is working. But the problem is when the path is just localhost:9000 it is not redirecting to login. Earlier it used to route to login if the user enters a wrong path or not logged in. So now when I just type the url localhost:9000, it is showing the blank page and the error. Here is how I route.
var demoClientApp=angular.module('demoClientApp', ['ngCookies']);
demoClientApp.config(['$routeProvider', '$locationProvider',
function($routeProvider,$locationProvider) {
$routeProvider
.when('/login', {
templateUrl: 'login.html',
controller: 'LoginController'
})
.when('/register', {
templateUrl: 'register.html',
controller: 'RegisterController'
})
.when('/home', {
templateUrl: 'views/dashboard.html',
controller: 'DashboardCtrl'
})
.otherwise({
redirectTo: '/login'
});
}]);