I have a bootstrap themed angular app that has 2 radio buttons that bring up separate pages. Both can't be selected at the same time. When one is selected I want the btn btn-primary active to be the class values while the other one which is not selected has the class attributes btn btn-secondary.
I've spent hours google searching and playing around with different things and nothing has worked. Here's the plunker with my attempt so far. Any idea what I'm doing wrong?
A few things that are off-
You didn't include AngularJS. Of course it won't run, it's not there at all...
You're trying to use ng-class as a class the wrong way. Aside from the fact that using the ng-class attribute is generally easier*, you have it formatted wrong. You have ng-class=!view.isSelected ? 'btn-primary active' : 'btn-secondary', which should be ng-class:!view.isSelected ? 'btn-primary active' : 'btn-secondary'. Note the : instead of the = sign.
*This is not to say that you can't use the class version of ng-class (it's definitely valid to do so), but I'd generally make the distinction to have static classes in the class attribute, and anything dynamic in ng-class.
You have no controller. While technically you can cause the view to "truthily" create a scope variable by negating undefined (which is a very sloppy way of doing it), this will at best allow you to have one binary/toggle radio set, or a set of mutually allowed toggles. You should have, in your controller, a scope variable that holds which button is pressed (each button setting it on click to something to indicate it's now them), and having ng-class evaluate for that.
in your plunker demo didn't use bootstrap to use class btn btn-primary btn-secondary so add bootstrap link.
and invalid expression for ng -class. should use
class="btn" ng-class="button.view1Selected ? 'btn-primary active' : 'btn-secondary'"
insted of
class="btn ng-class:button.view1Selected ? 'btn-primary active' : 'btn-secondary'"
Better to use an array to store all view buttons info with selected property that initially false.
like:
$scope.views = [{isSelected: false},{isSelected: false}];// index based 0 for 1st button ...` or can be specific property based as you need
and when clicked on any button then set that button is selected and set class according to your condition.Call a function to set specific button active flag.
like: ng-click="selectedButton(1)"
and check to set class like: ng-class="views[0].isSelected ? 'btn-primary active' : 'btn-secondary'"
selectedButton function like:
$scope.selectedButton = function(viewIndex){
angular.forEach($scope.views, function(view, index) {
if(viewIndex == index) {
view.isSelected = ! view.isSelected;
} else {
view.isSelected = false;
}
});
};
See Plunker Demo
Related
I'm trying to have a checkbox, so when you click on the default FontAwesome empty box (fa-square-o) it gets changed with this icon (fa-check-square-o).
How can I do that with AngularJS? I need to put a function in the controller and call it from ng-click? What would be the correct function?
I found what I need in Jquery but would love to just use angular for it:
$("#checkBoxOn").click(function(event) {
$(this).find('i').toggleClass('fa-check-square-o');
If possible help me convert this in Angular!
Two parts:
You need something that holds a boolean if something is checked. We can do that using an expression like this: ng-click="toggle = !toggle". Basically, each time you click the element with that directive, toggle will become what it wasn't before.
You can use a ternary operator to set the class: i ng-class="toggle ? 'fa-check-square-o' : 'square-o'"></i>
Together, this might become something like:
<span ng-click="toggle = !toggle">
<i ng-class="toggle ? 'fa-check-square-o' : 'square-o'"></i>
Some text with the toggle here, that is also clickable.
</span>
Based on your other question, to hide another element based on this, you can add ngHide to it:
<table ng-hide="toggle">
This question is similar to AngularJS toggle class using ng-class, but that does not answer the toggling portion.
Just to make it more interesting, you could do something with unicode instead if you don't want the extra dependencies. This uses ngShow and ngHide.
<span ng-click="toggle=!toggle">
<span ng-show="toggle">☐</span>
<span ng-hide="toggle">☑</span>
Some text with the checkbox here
</span>
You don't need to use jQuery, use ngClass, to change the classes according to what a variable holds
<checkbox ng-click="value = !value">
<span class="fa" ng-class="{'fa-square-o': !value , 'fa-check-square-o': value}"></span>
You need ngChecked.
When you change the checkbox, apply your method:
ng-checked="MyMethod()"
The most easiest example:
input type='checkbox' ng-checked='MyMethod()' />
Script:
$scope.MyMethod = function() {
//your logic
return true; //checked
}
I am using a function which is returning an integer value based on that I am looping it to generate html divs. Please see the below code:
<div data-bind="foreach: new Array(getCartTotalParam())">
<div data-bind='text : $index()+1, css: { active: getCartParam("summary_count") >= $index()+1 }'></div>
</div>
the getCartTotalParam() is returning an integer let say 21, so I am generating 21 divs inside it.
Now I want to add a css class (as active).
The logic is I have used css binding which is call getCartParam("summary_count"). It checks the returned value and the loop's iteration index and based on this comparison I want to add css class.
But my code is not working. I have referred the doc but can't figure out what was missing.
I don't see anything wrong with your code given above. There might be a mistake in your logic.
The css binding with the above-given (question) syntax would apply active css if the given expression getCartParam("summary_count") >= $index()+1 is true otherwise it will not apply anything.
Here is working jsbin
You should change your $index()+1 as ($index()+1) in your <div> tag like this,
<div data-bind='text : $index()+1, css: { active: getCartParam("summary_count") >= ($index()+1) }'></div>
Here is a working DEMO for you: http://jsfiddle.net/GSvnh/5689/
Hope this helps!
I have a div like this
<div ng-model="star"></div>
I want to add a class 'active' to this div in angular js.
I tried like this in my angular js controller
$scope.star.addClass('active');
But,I am getting error. I am new to angular js. please help me...
Try this:
<div ng-class="{ active: star == 'yes', 'in-active': star == 'no' }" ng-bind="star"></div>
You can have one or more classes assigned based on the expression (true or false).
If a class name has a hyphen, enclose in single quote.
This is a better approach and preferred than changing in controller.
Also, because this is a div, you have to do ng-bind, not ng-model.
ng-model works on fields like input.
UPDATE:
Since you insist on changing the class in code, here it is:
$("div[ng-bind='star']").addClass('active');
If you want to access change class dynamically then you can put watch on your star variable in your controller like below :
$.watch('star',function(newVal, oldVal){
// put your code to here based on new value and old value
// you can add class to your div like :
// angular.element("div[ng-bind='star']").addClass('active');
});
Using a variable to control your class
<div ng-class="customClass"></div>
in controller
$scope.customClass = 'active custom-class1';
so, you can use if-else to change class name
if (something) {
$scope.customClass = 'active custom-class1';
} else {
$scope.customClass = 'deactive custom-class2';
}
I am trying to figure out a simple way to swap a link in a button in angular js, I have properties available in the scope that I can use, but I am not sure how to implement. The link I am trying to alter is in the 'onClick' attribute in the button. Thanks again in advance, here is my code:
<div class="instructions-button">
<button
type="button"
class="halo-modal-action-button external-instructions"
onClick="window.open('https://mysite/foo');"
window="new"
ng-disabled=""
ng-click=""
data-bs-enabled=""
>Instructions</button>
</div>
I am trying to make a simple conditional to show either:
'mysite/foo || mysite/bar'
but am not sure how to make it work.
Don't use onClick event on the button, although it might feel weird to you, since you are coming from vanilla JS, you should use ng-click.
<button
class="halo-modal-action-button external-instructions"
ng-disabled=""
ng-click="openWindow()"
data-bs-enabled=""
>Instructions</button>
and inside controller
$scope.openWindow = function(){
if ( condition ) {
open('http:foo.bar')
} else {
open('http:bar.foo')
}
}
Two simple ways to do this.
1) Two buttons and show one conditionally with ng-show.
<button ... ng-show="conditionShowFooTrue" >
<button ... ng-show="conditionShowBarTrue" >
2) Use ng-click to call a scope function to conditionally sets the url as the other answers have suggested.
If you're using routes, you can just use ng-click like #Akxe said, and with the $location service in your controller, do:
$scope.changeLocation = function(){
if(condition)
{
$location.path('/foo');
} else
{
$location.path('/bar');
}
};
I think that would be the Angular way to do this.
I have this
body
div(ng-class="toggle ? 'nav-open' : 'nav'")
.container
.logo
img(src='images/logo.png')
span Motto
ul(ng-class="toggle ? 'menu-open' : 'menu'")
li
a(href="#!/", ng-click='toggle = !toggle') Home
li
a(href="#!/products", ng-click='toggle = !toggle') Products
activated first by this
button.upon(type="button", ng-click='toggle = !toggle') Menu
and you see it closes clickin on a link. The problem is that when I close it by a click it doesnt' work anymore, must refresh the page in order to do it, and using this while on mobile it just closes without changing the page. What am I doing wrong and/or is there any better way to achieve what I'm doin?
Check out some of the examples in the docs - there's a few different ways to use ngClass. https://docs.angularjs.org/api/ng/directive/ngClass
I think this syntax should probably work for your case:
ng-class="{'nav-open': toggle, 'nav': !toggle}"
ng-class="{'menu-open': toggle, 'menu': !toggle}"
Essentially you're providing ngClass with a configuration object that says:
"Apply the nav-open class when toggle is true, and apply the nav class when toggle is false".