use expression as ng-class in angular js - javascript

I have unordered list in which I want different different class for each li which will be dynamic.
my code is
$scope.className = ['clr1','clr2','clr3','clr4','clr5'];
My Html :
<div>
<ul>
<li ng-repeat="item in mainitem" ng-style="{color: color[$index]}" >
<span ng-class="{className [$index]}">{{ item.name}}</span> // At this line I ahve added dynamic class but its not working
</li>
</ul>
</div>
This is not applied class as per classname array !!
Is there any issue with this code ? Thanks for helping !!

you can use [] instead of {}
<div>
<ul>
<li ng-repeat="item in mainitem" ng-style="{color: color[$index]}" >
<span ng-class="[className[$index]]">{{ item.name}}</span>
</li>
</ul>
</div>
for example of working see this codepen codepen

As the code is posted in the question, you are missing an opening brace when evaluating the class name
<span ng-class="{className [$index]}">{{ item.name}}</span>
should be
<span ng-class="{{className [$index]}">{{ item.name}}</span>
when it doesn't evaluate, you will end up with no class at all

Related

Append CSS class to element

I have a model, i.e. like this:
[{
"Name" : "Foo",
"CssClass" : "class1"
},
{
"Name" : "Bar",
"CssClass" : "class2"
}]
Which is presented using the following template:
<li v-for="foobar in model">
<span class="otherclass anotherclass">{{foobar.Name}}</span>
</li>
How could I append the CssClass property to the span?
I know you could do :class="{ active: isActive }" (as per the documentation), but this uses a predefined class called active, whereas I want to append the class name from the model.
I tried using <span class="otherclass anotherclass" :class="foobar.CssClass"> but that didn't add CssClass to class at all.
How could I go about making this work so that the <span> will effectively be rendered as <span class="otherclass anotherclass class1"> (for the first model entry)? And how could I also use a default value in case CssClass is not defined?
You can append the class like so
<li v-for="foobar in model">
<span :class="foobar.CssClass" class="otherclass anotherclass">{{foobar.Name}}</span>
</li>
I ran into the same issue in the past. During render all three classes will combine into one class="my_class_from_foobar otherclass anotherclass"
You can pass an object or an array to :class. You can also use class and :class together and Vue will resolve both correctly without issues:
<li v-for="foobar in model">
<span class="otherclass anotherclass" :class="[foobar.CssClass]">{{foobar.Name}}</span>
</li>
<li v-for="foobar in model">
<span :class="'otherclass anotherclass ' + foobar.CssClass">{{foobar.Name}</span>
</li>
<li v-for="foobar in model">
<span :class="foobar.CssClass" class="otherclass anotherclass">{{foobar.Name}</span>
</li>
Both should work

Angular ngFor li html element set style

<ul class="days">
<liclass="day" *ngFor="let day of currentDays; let i = index"(click)="setStyle()">
{{day}}
</li>
</ul>
My question is how to add style to exactly specified element in list, when i use ngClass styles are added to all elements? It posible using method add pass reference to element and set style?
You can conditionally set using ngStyle. refer the following code.
<div [ngStyle]="{'background-color':isTrue === true ? 'green' : 'red' }"></<div>
Or refer the stackblitz link for another solution.
enter link description here
You can set different class to each element
<ul class="days">
<li *ngFor="let day of currentDays; let i = index" [class]="'day'+(index+1)">
{{day}}
</li>
</ul>
in css set style to each one:
.day1{
...
}
You can use
<li [class.your-class]="expression">
For example:
<li [class.mat-elevation-z10]="i === selectedElement">
Or you can use ngClass with expressions
<li [ngClass]="{'first': true, 'second': true, 'third': false}">
Where first, second and third are your classes and true, true, false are the expressions. In the example above, both first and second class would be applied.
You can use ngClass itself with conditions. You can replace RequiredDay string with variable.
<ul class="days">
<li [ngClass]="{'desiredClass':day === 'RequiredDay'}" *ngFor="let day of currentDays; let i = index" (click)="setStyle()">
{{day}}
</li>
</ul>

How to get the string in div onclick attribute with jQuery?

I have a banner which is something like this:
<ul id="carousel">
<li id="item1">
<div onclick="window.open('mylinkhere.com,'_blank')" style="cursor:pointer;margin-left:-436px;width:996px;height:100%">
</div>
</li>
<li id="item2">
<div onclick="window.open('myotherlinkhere.com,'_blank')" style="cursor:pointer;margin-left:-436px;width:996px;height:100%">
</div>
</li>
</ul>
I need to access the links written in onclick attribute i.e. mylinkhere.com
I tried
var banners = $($("#carousel")[0]).children().filter("li");
var b_items = $(banners[0]).children().filter("div");
attr_val = $(".b_items")[0].attr("onclick");
But i couldn't. By the way i don't know from the start that which item will be in the carousel because it's randomized by another function. So i cannot access them with item1 item2 ect.
Thanks.
You can set the url in a data attribute and read easily like this:
<ul id="carousel">
<li id="item1">
<div data-url="mylinkhere.com" onclick="window.open($(this).data('url'),'_blank')" style="cursor:pointer;margin-left:-436px;width:996px;height:100%">
</div>
</li>
<li id="item2">
<div data-url="myotherlinkhere.com" onclick="window.open($(this).data('url'),'_blank')" style="cursor:pointer;margin-left:-436px;width:996px;height:100%">
</div>
</li>
</ul>
To read:
var b_items = $(banners[0]).children().filter("div");
var link = $(b_items).data('url');
See this fiddle:
http://jsfiddle.net/Pontual/pugytfwu/
For me, the following code works as expected (Chrome 44, jQuery 2.1.3):
var s = jQuery('#carousel li div');
s.each(function(i,node) {
alert(jQuery(node).attr('onclick'));
});
http://jsfiddle.net/aavf1450/
Possible problem is that in your code, you do not wrap the element into $(...), so .attr() is not a valid function, as using indexers on a jQuery object returns raw HTML elements.

simple tab system in Angularjs error

So I'm trying to create a simple tab system in angularjs, but whenever I try to bind data on ng-click on the a or li tags of my template I get this error:
Syntax Error: Token '' {1} at column {2} of the expression [{3}] starting at [{4}].
I been searching for hours can't figure out what's causing the problem
here's my code:
<div id="tabs">
<ul>
<li ng-repeat="file in files" ng-click="tab = {{$index}}">
{{file.file_name}}.{{file.file_extension}}
</li>
</ul>
</div>
<!-- tab container -->
<div class="tab-content">
<div class="tab" ng-repeat="doc in files" ng-show="tab == {{$index}}">
{{doc.file_name}}.{{doc.file_extension}}
</div>
</div>
I have tried wrapping {{$index}} in single quotes, that fixes the problem but the tabs don't work when clicked.
You could move this out of the inline logic and into a function, for example $scope.setTabIndex, like so:
$scope.setTabIndex = function(index) {
$scope.tab = index;
}
And then in your markup:
<li ng-repeat="file in files" ng-click="setTabIndex($index)">
And for the ng-show, just remove the curly braces around $index:
<div class="tab" ng-repeat="doc in files" ng-show="tab == $index">
Here's as jsBin

How to apply toggle?

here is my code of html on which i have to apply togle
<div class="contact-links contact-info">
<ul>
<li class="contact-link-list toggle-sub"><a data-toggle-handler="contact-details-2" href="#">Agent Details</a>
<ul data-toggle-group="contact-details-2" class="senf hidden">
<ul style="float:left">
<li style="margin-right:20px;">Office hours</li>
<li style="margin-right:20px;">Products offered</li>
<li>Languages</li>
<li>Visit Agent Site</a></li>
</ul>
<ul style='float:left; margin-left:-20px;'>
<li>Mon-Fri 9:00AM-5:00PM</li>
<li>Auto, Home</li>
<li>English, Spanish</li></ul>
</ul></li>
</ul>
</div>
and here is my code of jquery
$(document).ready(function(){
$(".contact-link-list").click(function(){
('.senf').slideToggle("fast");
});
});
here is my jsfiddle - http://jsfiddle.net/n7U6b/
please suggest me where i am wrong
There is no element in your example with a class of senf. Also you are missing a $ before your selector:
$('.senf').slideToggle("fast");
Edit
Here is an updated fiddle from your example. You are still missing the $, and you have a stray closing </a> tag in there, but you also need to add a class of senf to the item you want to toggle. For example:
<ul data-toggle-group="contact-details-2" class="hidden senf">
$(document).ready(function(){
$(".contact-link-list").click(function(){
$('.senf').slideToggle("fast");
});
});
You just missed a $ sign on '.senf'
Plus where is the item with class senf?? I might have missed it!

Categories