I am trying to toggle a class on click, please find my below code.
<li class="dropdown" data-ng-class="sign-open">
Sign In <b class="caret"></b>
<div class="dropdown-menu" style="padding: 15px;">
<form action="#" method="post" accept-charset="UTF-8" class="form-menu">
<input id="user_username" type="text" name="user[username]" size="33" placeholder="Username">
<input id="user_password" type="password" name="user[password]" size="33" placeholder="Password">
<label class="checkbox muted hidden-tablet">
<input type="checkbox">Remember Me</label>
<input class="btn span3" type="submit" name="commit" value="Sign In">
</form>
</div>
</li>
//sign in show-hide
$scope.signToogle = function () {
if ($scope.sign-open === "")
$scope.class = "open";
else
$scope.class = "";
}
this js funciton will addclass open so if ul has open class as it's parent then it will be visible.
But don't know how can I make that if click once then true and class append and if again clicked statement false and class will be removed.
You can use ng-class
<div ng-class="{active: is_active}">Some div</div>
<button ng-click="is_active = !is_active" ng-init="is_active=false">Click to toggle</button>
Set or reset $scope.is_active when you click
var app = angular.module('app', []);
app.controller('Main', function($scope) {
$scope.isOpen = false;
});
.item span {
display: none;
}
.item.close span.show-on-close {
display: block;
}
.item.open span.show-on-open {
display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app">
<div ng-controller="Main">
<pre>isOpen: {{isOpen | json}}</pre>
<span class="item" ng-class="{'open':isOpen, 'close':!isOpen}">
<span class="show-on-open">open</span>
<span class="show-on-close">close</span>
</span>
<button ng-click="isOpen = !isOpen">Toggle</button>
</div>
</div>
NOTE: variable in JavaScript can't contains the dash character
so rename your $scope.sign-open to $scope.signOpen
you don't even have to define a function in the controller, you can do it like this:
<span ng-class="{'open':!signOpen, '':signOpen}"></span>
on click handler
<button ng-click="signOpen = !signOpen">toggle</button>
Related
I have I form of inputs and using ng-repeat I add new inputs fields dinamically with button.
Each input is already completed by "text".
The problem :
When I insert new input field by button, the first input field clean out text.
I verify in my browser debugger and first element of my Items Array is not empty. Why it is not present on input ?
There is my HTML:
<div class="row">
<div class="col-sm-10">
<input ng-repeat="item in vm.myItemsArray"
name="myItemName"
class="form-control"
type="text"
ng-model="item.value"/>
</div>
<div class="col-sm-1 col-sm-offset-1">
<span ng-click="addItem()" class="glyphicon glyphicon-plus btn-sm btn-default"/>
</div>
</div>
AND JS :
// INITIALIZE INPUTS
vm.myItemsArray = [{value: "text"}];
// ADD NEW INPUTS
function addItem() {
vm.myItemsArray.push({value: "text"});
}
(function() {
'use strict';
angular
.module('angularExampleModule',[])
.controller('angularExampleController', ['$scope', angularExampleController]);
function angularExampleController($scope){
$scope.myItemsArray = [{value: "text"}];
$scope.addItem = function () {
$scope.myItemsArray.push({value: "text"});
}
}
})();
.input-element{
margin:10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="angularExampleModule" ng-controller="angularExampleController">
<div class="row">
<div class="col-sm-10 input-element" ng-repeat="item in myItemsArray">
<input name="myItemName" class="form-control" type="text" ng-model="item.value"/>
</div>
<div class="col-sm-1 col-sm-offset-1">
<button ng-click="addItem()" class="glyphicon glyphicon-plus btn-sm btn-default">Add Item</button>
</div>
</div>
OR Check this https://codepen.io/DeepaliK/pen/BqXqNB
I have this small AngularJS app, and most ng-click expressions work fine.
As an example I use submitted=true and submitted=false as hide and show for some buttons and to add a class show to the ingredients div. However, I can't get ng-click="submitted=false" on the last button .restart to work? On this click it should make the expression submitted=false, so the ingredients div will not have the show class anymore, and change the buttons.
How come?
Fiddle
var app = angular.module("app", []);
app.controller("IndexController", ["$scope", function($scope) {
this.list = {
"mandag": {}
};
this.submitted = false;
this.addFood = (day, title) => {
this.list[day] = {
"food": title,
"ingredients": []
};
}
this.removeFood = (day) => {
this.list[day] = {};
}
this.addIngredient = (day, ingredient) => {
this.list[day].ingredients.push({
"name": ingredient.name,
"amount": ingredient.amount,
"unit": ingredient.unit
});
}
this.UnSave = () => {
this.list.mandag = {};
}
}]);
.shopping {
width: 25%;
}
.ingredients {
opacity: .1;
transition: opacity 1s ease;
}
.show {
opacity: 1;
}
button {
border: none;
background: transparent;
}
<script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body ng-app="app" >
<div ng-controller="IndexController as vm">
<div style="margin: 20px; display: inline-block;" ng-repeat="(day, item) in vm.list track by $index" class="card-panel teal col s9">
<h3>{{day}}</h3>
<form class="" name="ret" ng-submit="vm.addFood(day, item.food)">
<input placeholder="" name="inputRet" class="inputRet" ng-disabled="submitted" type="text" ng-model="item.food" required/>
<button class="btn waves-effect waves-light addFood" ng-disabled="ret.$invalid" ng-click="submitted=true" ng-hide="submitted">Add</button>
<a class="btn waves-effect waves-light editFood" ng-click="submitted=false" ng-show="submitted"><i class="fa fa-pencil-square-o" aria-hidden="true"></i></a>
<a ng-click="vm.removeFood(day); submitted=false">remove</a>
</form>
<div class="ingredients off" ng-class="{'show': submitted}">
<p>Ingredienser:</p>
<ul>
<li ng-repeat="ingredient in item.ingredients track by $index">
<button class="btn waves-effect waves-light" ng-click="vm.removeIngredient(day, ingredient); submitted=true">Add</button>
<span>{{ingredient.name}} ({{ingredient.amount}} {{ingredient.unit}})</span>
</li>
</ul>
<form name="ingredients_form" class="input-field">
<input placeholder="Ingredient" type="text" name="ingredients_name" ng-model="vm.ingredient[$index].name" value="" required/>
<input placeholder="Amount" type="number" ng-model="vm.ingredient[$index].amount" value="" required/>
<select ng-model="vm.ingredient[$index].unit" required>
<option value="" disabled selected>Choose your option</option>
<option value="g" title="gram">g</option>
<option value="kg" title="kilogram">kg</option>
<option value="l" title="liter">l</option>
</select>
</form>
<button class="btn waves-effect waves-light" ng-disabled="ingredients_form.$invalid" ng-click="vm.addIngredient(day, vm.ingredient[$index]); vm.ingredient[$index] = ''; ">Add</button>
</div>
</div>
<button class="restart" ng-click="vm.UnSave(); submitted=false">Restart</button>
</div>
</body>
I have small form with 2 date input and one checklist which are all checkboxes. I couldn't figure it out how to require any of one checkbox in list for the form validation. If I add ng-required for an checkbox it repeats for all the check boxes. Can anyone know about this how to require the checkbox as required form element. but I don't want to choose all the element for making form valid. So if only one of the form field which are the checkboxes has been selected form must be valid otherwise invalid.
angular.module('frmApp', [
'ui.bootstrap', 'angularMoment'
])
.controller('Frm Controller', [
'$scope',
function($scope) {
$scope.invDets = $stateParams.details;
$scope.allowanceObj = {};
$scope.finCompWithLogo = [];
$scope.validUntil = new Date();
$scope.recentDate = new Date();
// Disable weekend selection
$scope.disabled = function(date, mode) {
return (mode === 'day' && (date.getDay() === 0 || date.getDay() === 6));
};
$scope.openedPayment = false;
$scope.openedAllowance = false;
$scope.openPayment = function($event, elementOpened) {
$scope.paymentDueDate = new Date();
/*$scope.openedPayment = !$scope.openedPayment;*/
$event.preventDefault();
$event.stopPropagation();
$scope[elementOpened] = !$scope[elementOpened];
};
$scope.openAllowance = function($event, elementOpened) {
$scope.allowanceDueDate = new Date();
/*$scope.openedAllowance = !$scope.openedAllowance;*/
$event.preventDefault();
$event.stopPropagation();
$scope[elementOpened] = !$scope[elementOpened];
};
$scope.dateOptions = {
'year-format': "'yy'",
'starting-day': 1,
'minDate': new Date()
};
$scope.doSomething = function (frm) {
$http.post('/someUrl', frm, config).then(successCallback, errorCallback); alert('Done something!!');
}
}
]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.1/css/font-awesome.min.css" rel="stylesheet"/>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<div>
<form name="frm1" novalidate>
<div class="row">
<div class="allowance-required-field">
<div class="box">
<div class="icon"> <i class="fa fa-calendar"></i> </div>
<div class="field-title">Payment</div>
<div class="input-group">
<input type="text" class="form-control" uib-datepicker-popup="dd-MM-yyyy" ng-click="openPayment($event, 'openedPayment')" ng-model="frm1.PaymentDueDate" show-weeks="false" is-open="openedPayment" datepicker-options="dateOptions" ng-required="true"
/>
<span class="input-group-btn">
<button class="btn btn-default" ng-click="openPayment($event, 'openedPayment')"><i class="fa fa-calendar"></i></button>
</span>
</div>
</div>
</div>
<div class="allowance-required-field">
<div class="box">
<div class="icon"> <i class="fa fa-calendar"></i> </div>
<div class="field-title">Delay</div>
<div class="input-group">
<input type="text" class="form-control" uib-datepicker-popup="dd-MM-yyyy" ng-click="openAllowance($event, 'openedAllowance')" ng-model="frm1.AllowanceDueDate" show-weeks="false" is-open="openedAllowance" min-date="recentDate" max-date="frm1.PaymentDueDate"
datepicker-options="dateOptions" ng-required="true" />
<span class="input-group-btn">
<button class="btn btn-default" ng-click="openAllowance($event, 'openedAllowance')"><i class="fa fa-calendar"></i></button>
</span>
</div>
</div>
</div>
<div class="pick-factoring-companies">
<div class="box">
<h2 class="text-center"> Choose One or More </h2>
<div class="text-center">
Select All
Deselect All
</div>
<ul ng-required="true">
<li ng-repeat="cmp in finCompWithLogo">
<div ng-if="cmp" class="finance-company">
<input id="{{'company-'+$index}}" type="checkbox" class="pick-faktoring" checklist-model="frm1.AllowanceCompanies" checklist-value="cmp.Identifier" ng-change="addCompany(cmp.Identifier)">
<label for="{{'company-'+$index}}">
<div class="img"> <img data-toggle="tooltip" data-placement="bottom" src="data:image/{{cmp.Logo[0].Type}};base64,{{cmp.Logo[0].Data}}" title="{{cmp.CompanyName}}"> </div>
<div class="title"> {{cmp.CompanyName}} </div>
</label>
</div>
</li>
</ul>
</div>
</div>
</div>
<div class="text-center">
Send
</div>
</form>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</div>
Mark the checkboxes as ng-required if the list is empty.
<label ng-repeat="role in roles">
<input ng-required="user.roles.length == 0"
type="checkbox"
checklist-model="user.roles"
checklist-value="role.id">
{{role.text}}
</label>
You can use checklist-change to set the validity or checklist-before-change to enable/disable selection.
Here's an example: http://jsfiddle.net/beradrian/nbwcbejw/
If you go for checklist-change and $setValidity then you should set this initially.
SOLVED! I have tried solves my problem.
<input id="{{'company-'+$index}}" type="checkbox" ng-model="cmp.Selected" ng-click="pushCompanyToArray(cmp)"
ng-required="frm1.AllowanceCompanies.length == 0"
ng-value="cmp.ID" ng-checked="all || cmp.Selected"/>
this solves my question. For anyone who couldn't find the solution this ng-required solved my problem.
<!-- code snippet of html -->
<div class="form-group">
<label for="items" class="col-sm-2 control-label">Items</label>
<div class="pull-left input-group">
<input type="text" class="form-control" ng-model="name">
<span class="input-group-btn">
<button type="button" class="btn btn-default" ng-click="addNewItem()">Add</button>
</span>
</div>
<div>
<ul class="pull-left">
<li ng-repeat="item in items">
<p style="display: inline-block">{{item}}</p>
<button type="button" class="btn btn-default" ng-click="deleteItem(item)">Delete</button>
</li>
</ul>
</div>
</div>
<div class="form-group">
<label for="anotherField" class="col-sm-2 control-label">Another Field</label>
<input type="text" class="form-control" ng-model="anotherField" id="anotherField">
</div>
/* code snippet in Augular controller */
$scope.addNewItem = function() {
if ($scope.name && $scope.name.trim()) {
if ($scope.items.indexOf($scope.name.trim()) >= 0) {
console.log('item name should not be duplicated');
} else {
$scope.items.push($scope.name.trim());
$scope.name = '';
}
}
};
$scope.deleteItem = function(item) {
var idx = $scope.items.indexOf(item);
if (idx >= 0) $scope.items.splice(idx, 1);
};
Above codes function properly, but the only problem is that when I create even on one new item, the label supposedly for "anotherField" just shows up immediately after this this very item (even misaligned with the label for "items"); when more items were created, it's simply placed at the end of the 1st item but not like a label for "anotherField" any more. Anybody can help to address this "strange" style issue?
Thanks,
Xihua
I am new at AngularJS and I am trying to figure out how to add and delete notes to the setup that I have created. I have tried a lot of different things but I cant figure out how to get it to work.
I have cleaned up my code so it should be easier to figure out.
Please take a look at the jsfiddle version or snippet of my code:
'use strict'
var app = angular.module('plunker', ['ui.sortable']);
app.controller('MainCtrl', function($scope) {
var i;
$scope.itemsList = {
items1: [],
items2: []
};
for (i = 0; i <= 5; i += 1) {
$scope.itemsList.items1.push({
'Id': i,
'Label': 'Item ' + i
});
}
$scope.sortableOptions = {
containment: '#sortable-container',
accept: function(sourceItemHandleScope, destSortableScope) {
return sourceItemHandleScope.itemScope.sortableScope.$id === destSortableScope.$id;
}
};
});
.as-sortable-item,
.as-sortable-placeholder {} #sortable-container {} .touch-mover {
float: right;
padding: 3px;
margin-top: 5px;
}
<html ng-app="plunker">
<head>
<script data-require="angular.js#1.2.x" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.17/angular.min.js" data-semver="1.2.17"></script>
<script src="https://rawgithub.com/a5hik/ng-sortable/master/dist/ng-sortable.js"></script>
</head>
<body ng-controller="MainCtrl">
<div id="sortable-container">
<div class="form-actions">
<div class="input-append">
<form>
<input class="span3" size="16" type="text" placeholder="Add a note">
<button class="btn btn-success" type="button" ng-disabled="" ng-click="">
Add Note
</button>
</form>
</div>
</div>
<div class="sortable-row" as-sortable="sortableOptions" ng-model="itemsList.items1">
<div ng-repeat="item in itemsList.items1" class="simpel-fighter-input" as-sortable-item>
<input class="category-form textboxid" type="text" name="input" ng-model="item.Label" placeholder="Deltager1">
<div as-sortable-item-handle class="touch-mover">MOVE ME</div>
<a ng-click="" href>
<div class="touch-mover">DELETE</div>
</a>
<input class="category-form textboxid" style="float:right" type="text" name="input" ng-model="item.Label2" placeholder="Deltager2">
</div>
</div>
</div>
</body>
</html>
For adding a note just insert into your controller MainCtrl this function:
$scope.addNote = function() {
$scope.itemsList.items1.push({"Id" : $scope.itemsList.items1.length, "Label": "Item " + $scope.itemsList.items1.length})
}
and edit your form (add ng-model to input and ng-click function to button) like:
<input class="span3" size="16" type="text" ng-model="noteText" placeholder="Add a note">
<button class="btn btn-success" type="button" ng-disabled="" ng-click="addNote()">
Add Note
</button>
For deleting use in you controller something like
$scope.deleteNote = function(index) {
$scope.itemsList.items1.splice(index,1)
}
and attach this function to the tag "DELETE" like
ng-click="deleteNote($index)"
I hope this helps.