Use local variable in template html without component Angular2+
up vote
0
down vote
favorite
I want to use a local variable into my html template to use it in css classes but without linking it with the component. I want to do that :
[local_html_variable = 1]
<div class="css-{{ local_html_variable }}">
Div #1
</div>
[local_html_variable + 1]
<div class="css-{{ local_html_variable }}">
Div #2
</div>
[local_html_variable + 1]
<div class="css-{{ local_html_variable }}">
Div #3
</div>
...
to get
<div class="css-1">
Div #1
</div>
<div class="css-2">
Div #2
</div>
<div class="css-3">
Div #3
</div>
...
Important : the number of div is dynamic.
But I don't achieve it. I tried with <ng-template let-localHtmlVariable>
but didn't work.. Any idea ?
html angular
add a comment |
up vote
0
down vote
favorite
I want to use a local variable into my html template to use it in css classes but without linking it with the component. I want to do that :
[local_html_variable = 1]
<div class="css-{{ local_html_variable }}">
Div #1
</div>
[local_html_variable + 1]
<div class="css-{{ local_html_variable }}">
Div #2
</div>
[local_html_variable + 1]
<div class="css-{{ local_html_variable }}">
Div #3
</div>
...
to get
<div class="css-1">
Div #1
</div>
<div class="css-2">
Div #2
</div>
<div class="css-3">
Div #3
</div>
...
Important : the number of div is dynamic.
But I don't achieve it. I tried with <ng-template let-localHtmlVariable>
but didn't work.. Any idea ?
html angular
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I want to use a local variable into my html template to use it in css classes but without linking it with the component. I want to do that :
[local_html_variable = 1]
<div class="css-{{ local_html_variable }}">
Div #1
</div>
[local_html_variable + 1]
<div class="css-{{ local_html_variable }}">
Div #2
</div>
[local_html_variable + 1]
<div class="css-{{ local_html_variable }}">
Div #3
</div>
...
to get
<div class="css-1">
Div #1
</div>
<div class="css-2">
Div #2
</div>
<div class="css-3">
Div #3
</div>
...
Important : the number of div is dynamic.
But I don't achieve it. I tried with <ng-template let-localHtmlVariable>
but didn't work.. Any idea ?
html angular
I want to use a local variable into my html template to use it in css classes but without linking it with the component. I want to do that :
[local_html_variable = 1]
<div class="css-{{ local_html_variable }}">
Div #1
</div>
[local_html_variable + 1]
<div class="css-{{ local_html_variable }}">
Div #2
</div>
[local_html_variable + 1]
<div class="css-{{ local_html_variable }}">
Div #3
</div>
...
to get
<div class="css-1">
Div #1
</div>
<div class="css-2">
Div #2
</div>
<div class="css-3">
Div #3
</div>
...
Important : the number of div is dynamic.
But I don't achieve it. I tried with <ng-template let-localHtmlVariable>
but didn't work.. Any idea ?
html angular
html angular
edited Nov 22 at 14:15
asked Nov 22 at 13:56
MatDepInfo
235
235
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
You can use *ngFor structural directive
<div *ngFor="let value of [1,2,3]" class="css-{{value}}">
DIV #{{value}}
</div>
Why didn't I think of this? I wonder :D
– SiddAjmera
Nov 22 at 14:12
In fact I can't use ngFor in my case
– MatDepInfo
Nov 22 at 14:16
1
@MatDepInfo, please consider adding all the relevant details and conditions upfront, so as to avoid additional noise to the thread.
– SiddAjmera
Nov 22 at 14:23
add a comment |
up vote
0
down vote
Here is a very situational answer that takes advantages of truhy/falsy values :
<ng-container *ngIf="1 as i">
Number is {{ i }}
</ng-container>
Use it in your classes in the container itself :
<div class="css-{{ i }}">With interpolation</div>
<div [class]="'css-' + i">With input</div>
Here is the stackblitz : https://stackblitz.com/edit/angular-3wm4en?file=src%2Fapp%2Fapp.component.html
EDIT explanation :
In javascript, every value can be transalted to boolean : they are truthy or falsy values.
The quick boolean parse operator is the !!
double negation.
Let's try :
console.log(!!'');
console.log(!!0);
console.log(!!5);
When we use this notation, the evaluation use the same principle : it tests if the given value is truthy or falsy. In numbers, 1 being truthy, the test checks out, and the as i
notation simply creates a template variable.
For information, falsy values are 0, '', null, undefined, infinity, NaN
.
Whoa. That's amazing. I didn't know that. Niceee :) Can you please add some explanation to your post BTW? What exactly are you using here? I haven't seen anything like this before.
– SiddAjmera
Nov 22 at 14:10
Ok but how to increment it ?
– MatDepInfo
Nov 22 at 14:11
@MatDepInfo With ai + 1
in your interpolation. You can try withi++
and it'll show you why you shouldn't use that !
– trichetriche
Nov 22 at 14:13
@SiddAjmera see my edit
– trichetriche
Nov 22 at 14:16
@MatDepInfo you should provide a Minimal, Complete, and Verifiable example of what you expect, random examples like that are always source of mistakes. If you don't know the number of divs, do you at least have some number that says how much divs you have ?
– trichetriche
Nov 22 at 14:17
|
show 2 more comments
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
You can use *ngFor structural directive
<div *ngFor="let value of [1,2,3]" class="css-{{value}}">
DIV #{{value}}
</div>
Why didn't I think of this? I wonder :D
– SiddAjmera
Nov 22 at 14:12
In fact I can't use ngFor in my case
– MatDepInfo
Nov 22 at 14:16
1
@MatDepInfo, please consider adding all the relevant details and conditions upfront, so as to avoid additional noise to the thread.
– SiddAjmera
Nov 22 at 14:23
add a comment |
up vote
2
down vote
You can use *ngFor structural directive
<div *ngFor="let value of [1,2,3]" class="css-{{value}}">
DIV #{{value}}
</div>
Why didn't I think of this? I wonder :D
– SiddAjmera
Nov 22 at 14:12
In fact I can't use ngFor in my case
– MatDepInfo
Nov 22 at 14:16
1
@MatDepInfo, please consider adding all the relevant details and conditions upfront, so as to avoid additional noise to the thread.
– SiddAjmera
Nov 22 at 14:23
add a comment |
up vote
2
down vote
up vote
2
down vote
You can use *ngFor structural directive
<div *ngFor="let value of [1,2,3]" class="css-{{value}}">
DIV #{{value}}
</div>
You can use *ngFor structural directive
<div *ngFor="let value of [1,2,3]" class="css-{{value}}">
DIV #{{value}}
</div>
answered Nov 22 at 14:11
Suresh Kumar Ariya
4,1161215
4,1161215
Why didn't I think of this? I wonder :D
– SiddAjmera
Nov 22 at 14:12
In fact I can't use ngFor in my case
– MatDepInfo
Nov 22 at 14:16
1
@MatDepInfo, please consider adding all the relevant details and conditions upfront, so as to avoid additional noise to the thread.
– SiddAjmera
Nov 22 at 14:23
add a comment |
Why didn't I think of this? I wonder :D
– SiddAjmera
Nov 22 at 14:12
In fact I can't use ngFor in my case
– MatDepInfo
Nov 22 at 14:16
1
@MatDepInfo, please consider adding all the relevant details and conditions upfront, so as to avoid additional noise to the thread.
– SiddAjmera
Nov 22 at 14:23
Why didn't I think of this? I wonder :D
– SiddAjmera
Nov 22 at 14:12
Why didn't I think of this? I wonder :D
– SiddAjmera
Nov 22 at 14:12
In fact I can't use ngFor in my case
– MatDepInfo
Nov 22 at 14:16
In fact I can't use ngFor in my case
– MatDepInfo
Nov 22 at 14:16
1
1
@MatDepInfo, please consider adding all the relevant details and conditions upfront, so as to avoid additional noise to the thread.
– SiddAjmera
Nov 22 at 14:23
@MatDepInfo, please consider adding all the relevant details and conditions upfront, so as to avoid additional noise to the thread.
– SiddAjmera
Nov 22 at 14:23
add a comment |
up vote
0
down vote
Here is a very situational answer that takes advantages of truhy/falsy values :
<ng-container *ngIf="1 as i">
Number is {{ i }}
</ng-container>
Use it in your classes in the container itself :
<div class="css-{{ i }}">With interpolation</div>
<div [class]="'css-' + i">With input</div>
Here is the stackblitz : https://stackblitz.com/edit/angular-3wm4en?file=src%2Fapp%2Fapp.component.html
EDIT explanation :
In javascript, every value can be transalted to boolean : they are truthy or falsy values.
The quick boolean parse operator is the !!
double negation.
Let's try :
console.log(!!'');
console.log(!!0);
console.log(!!5);
When we use this notation, the evaluation use the same principle : it tests if the given value is truthy or falsy. In numbers, 1 being truthy, the test checks out, and the as i
notation simply creates a template variable.
For information, falsy values are 0, '', null, undefined, infinity, NaN
.
Whoa. That's amazing. I didn't know that. Niceee :) Can you please add some explanation to your post BTW? What exactly are you using here? I haven't seen anything like this before.
– SiddAjmera
Nov 22 at 14:10
Ok but how to increment it ?
– MatDepInfo
Nov 22 at 14:11
@MatDepInfo With ai + 1
in your interpolation. You can try withi++
and it'll show you why you shouldn't use that !
– trichetriche
Nov 22 at 14:13
@SiddAjmera see my edit
– trichetriche
Nov 22 at 14:16
@MatDepInfo you should provide a Minimal, Complete, and Verifiable example of what you expect, random examples like that are always source of mistakes. If you don't know the number of divs, do you at least have some number that says how much divs you have ?
– trichetriche
Nov 22 at 14:17
|
show 2 more comments
up vote
0
down vote
Here is a very situational answer that takes advantages of truhy/falsy values :
<ng-container *ngIf="1 as i">
Number is {{ i }}
</ng-container>
Use it in your classes in the container itself :
<div class="css-{{ i }}">With interpolation</div>
<div [class]="'css-' + i">With input</div>
Here is the stackblitz : https://stackblitz.com/edit/angular-3wm4en?file=src%2Fapp%2Fapp.component.html
EDIT explanation :
In javascript, every value can be transalted to boolean : they are truthy or falsy values.
The quick boolean parse operator is the !!
double negation.
Let's try :
console.log(!!'');
console.log(!!0);
console.log(!!5);
When we use this notation, the evaluation use the same principle : it tests if the given value is truthy or falsy. In numbers, 1 being truthy, the test checks out, and the as i
notation simply creates a template variable.
For information, falsy values are 0, '', null, undefined, infinity, NaN
.
Whoa. That's amazing. I didn't know that. Niceee :) Can you please add some explanation to your post BTW? What exactly are you using here? I haven't seen anything like this before.
– SiddAjmera
Nov 22 at 14:10
Ok but how to increment it ?
– MatDepInfo
Nov 22 at 14:11
@MatDepInfo With ai + 1
in your interpolation. You can try withi++
and it'll show you why you shouldn't use that !
– trichetriche
Nov 22 at 14:13
@SiddAjmera see my edit
– trichetriche
Nov 22 at 14:16
@MatDepInfo you should provide a Minimal, Complete, and Verifiable example of what you expect, random examples like that are always source of mistakes. If you don't know the number of divs, do you at least have some number that says how much divs you have ?
– trichetriche
Nov 22 at 14:17
|
show 2 more comments
up vote
0
down vote
up vote
0
down vote
Here is a very situational answer that takes advantages of truhy/falsy values :
<ng-container *ngIf="1 as i">
Number is {{ i }}
</ng-container>
Use it in your classes in the container itself :
<div class="css-{{ i }}">With interpolation</div>
<div [class]="'css-' + i">With input</div>
Here is the stackblitz : https://stackblitz.com/edit/angular-3wm4en?file=src%2Fapp%2Fapp.component.html
EDIT explanation :
In javascript, every value can be transalted to boolean : they are truthy or falsy values.
The quick boolean parse operator is the !!
double negation.
Let's try :
console.log(!!'');
console.log(!!0);
console.log(!!5);
When we use this notation, the evaluation use the same principle : it tests if the given value is truthy or falsy. In numbers, 1 being truthy, the test checks out, and the as i
notation simply creates a template variable.
For information, falsy values are 0, '', null, undefined, infinity, NaN
.
Here is a very situational answer that takes advantages of truhy/falsy values :
<ng-container *ngIf="1 as i">
Number is {{ i }}
</ng-container>
Use it in your classes in the container itself :
<div class="css-{{ i }}">With interpolation</div>
<div [class]="'css-' + i">With input</div>
Here is the stackblitz : https://stackblitz.com/edit/angular-3wm4en?file=src%2Fapp%2Fapp.component.html
EDIT explanation :
In javascript, every value can be transalted to boolean : they are truthy or falsy values.
The quick boolean parse operator is the !!
double negation.
Let's try :
console.log(!!'');
console.log(!!0);
console.log(!!5);
When we use this notation, the evaluation use the same principle : it tests if the given value is truthy or falsy. In numbers, 1 being truthy, the test checks out, and the as i
notation simply creates a template variable.
For information, falsy values are 0, '', null, undefined, infinity, NaN
.
console.log(!!'');
console.log(!!0);
console.log(!!5);
console.log(!!'');
console.log(!!0);
console.log(!!5);
edited Nov 22 at 14:19
answered Nov 22 at 14:06
trichetriche
24.7k42050
24.7k42050
Whoa. That's amazing. I didn't know that. Niceee :) Can you please add some explanation to your post BTW? What exactly are you using here? I haven't seen anything like this before.
– SiddAjmera
Nov 22 at 14:10
Ok but how to increment it ?
– MatDepInfo
Nov 22 at 14:11
@MatDepInfo With ai + 1
in your interpolation. You can try withi++
and it'll show you why you shouldn't use that !
– trichetriche
Nov 22 at 14:13
@SiddAjmera see my edit
– trichetriche
Nov 22 at 14:16
@MatDepInfo you should provide a Minimal, Complete, and Verifiable example of what you expect, random examples like that are always source of mistakes. If you don't know the number of divs, do you at least have some number that says how much divs you have ?
– trichetriche
Nov 22 at 14:17
|
show 2 more comments
Whoa. That's amazing. I didn't know that. Niceee :) Can you please add some explanation to your post BTW? What exactly are you using here? I haven't seen anything like this before.
– SiddAjmera
Nov 22 at 14:10
Ok but how to increment it ?
– MatDepInfo
Nov 22 at 14:11
@MatDepInfo With ai + 1
in your interpolation. You can try withi++
and it'll show you why you shouldn't use that !
– trichetriche
Nov 22 at 14:13
@SiddAjmera see my edit
– trichetriche
Nov 22 at 14:16
@MatDepInfo you should provide a Minimal, Complete, and Verifiable example of what you expect, random examples like that are always source of mistakes. If you don't know the number of divs, do you at least have some number that says how much divs you have ?
– trichetriche
Nov 22 at 14:17
Whoa. That's amazing. I didn't know that. Niceee :) Can you please add some explanation to your post BTW? What exactly are you using here? I haven't seen anything like this before.
– SiddAjmera
Nov 22 at 14:10
Whoa. That's amazing. I didn't know that. Niceee :) Can you please add some explanation to your post BTW? What exactly are you using here? I haven't seen anything like this before.
– SiddAjmera
Nov 22 at 14:10
Ok but how to increment it ?
– MatDepInfo
Nov 22 at 14:11
Ok but how to increment it ?
– MatDepInfo
Nov 22 at 14:11
@MatDepInfo With a
i + 1
in your interpolation. You can try with i++
and it'll show you why you shouldn't use that !– trichetriche
Nov 22 at 14:13
@MatDepInfo With a
i + 1
in your interpolation. You can try with i++
and it'll show you why you shouldn't use that !– trichetriche
Nov 22 at 14:13
@SiddAjmera see my edit
– trichetriche
Nov 22 at 14:16
@SiddAjmera see my edit
– trichetriche
Nov 22 at 14:16
@MatDepInfo you should provide a Minimal, Complete, and Verifiable example of what you expect, random examples like that are always source of mistakes. If you don't know the number of divs, do you at least have some number that says how much divs you have ?
– trichetriche
Nov 22 at 14:17
@MatDepInfo you should provide a Minimal, Complete, and Verifiable example of what you expect, random examples like that are always source of mistakes. If you don't know the number of divs, do you at least have some number that says how much divs you have ?
– trichetriche
Nov 22 at 14:17
|
show 2 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53432556%2fuse-local-variable-in-template-html-without-component-angular2%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown