onclick button get the value of selected (checkbox) value - angularjs

<div class="chks" ng-repeat="request in pendingRequest">
<input type="checkbox" name="chk" ng-model="pendingRequest" id="chk1" ng-click="pendingUser(request.user_id)">
<label for="chk1">{{request.first_name}} {{request.last_name}}</label>
</div>
<button type="button" ng-click="checkVal()" class="btn btn-default next-step"><span class="next-step">Resend Linking Request</span></button>
var selectUser = '';
$scope.pendingUser = function(user) {
selectUser = user;
}
$scope.checkVal = function() {
if (selectUser) {
alert(selectUser);
} else {
alert("CheckBox is not checked.");
}
}
How can I check on click checkVal button function has user_id.
on bases checkbox click get the user_id in Angularjs.

Use an array to push the value entries from the checkbox.
$scope.nameSelected= [];
Give a single value for the checkbox. I'm assuming that to be the first name.
$scope.getAllSelected = function(){
angular.forEach($scope.first_name,function(key,value){
if(key)
$scope.nameSelected.push(value)
});
console.log($scope.nameSelected)
}
Remove the unwanted ng-click from the checkbox input entry. getAllSelected can be done in the ng-click on the submit button you've there.

<div class="chks" ng-repeat="request in pendingRequest">
<input type="checkbox" name="chk" ng-model="pendingRequest" id="chk1" ng-click="pendingUser(request.user_id)">
<label for="chk1">{{request.first_name}} {{request.last_name}}
</label>
</div>
Why would you ng-repeat over the same model than your checkbox is ? Can you provide a plunker or something.
$scope.checkVal = function(){
if (selectUser) {
alert(selectUser);
} else {
alert("CheckBox is not checked.");
}
}
The selectUser is undefined there, is it supposed to be a single user (and you should pass it in params then), or the whole array pendingRequest ?
If you want to check if each request is selected then you could just bind your checkbox to request.selected and return in the check request.selected
Which would be something like that :
$scope.checkVal = function(){
angular.forEach($scope.pendingRequest, function(request) {
if (request.selected)
alert(request)
else
alert("Not selected")
})
}

Related

Issue which showing the image

What am I trying?
On button clicking showing the progress bar and hiding the button that was clicked.
Problem
Button is not hiding and image is not showing. I inspected the element and I can confirm that the image is given correct url.
My AngularJs Html is below
<form ng-submit="doLogin();" novalidate name='loginForm'>
<input type="text" name="EmailAddress" ng-model="credentials.EmailAddress" required/>
<button type="submit" ng-disabled="loginForm.$invalid" ng-show="{{RegisterShow}}">
Login
</button>
<img ng-src="{{imgProcessingPath}}" ng-show="{{imgProcessingShow}}">
</form>
Controller is below
myApp.controller('LoginController', ['$scope',
function($scope) {
$scope.imgProcessingPath = appUrl + "Images/ajax-loader.gif";
$scope.imgProcessingShow = false;
$scope.RegisterShow = true;
angular.extend($scope, {
doLogin: function() {
$scope.imgProcessingShow = true;
$scope.RegisterShow = false;
var data = {
"EmailAddress": $scope.credentials.EmailAddress
};
userModel.doLogin(data).then(function(response) {
$scope.imgProcessingShow = false;
$scope.RegisterShow = true;
},function(response) {
$scope.imgProcessingShow = false;
$scope.RegisterShow = true;
});
}
});
}
]);
Am I missing anything?
try removing the curly braces for ng-show in html file!
ng-show="RegisterShow"
alternatively you can also use
ng-if="RegisterShow"
ng-show should not have an expression, it acts like model variable,
Change it as,
<button type="submit" ng-disabled="loginForm.$invalid" ng-show="RegisterShow">
also
<img ng-src="{{imgProcessingPath}}" ng-show="imgProcessingShow">
I am posting this as an answer as I don't have the rep to comment.
Remove the brackets inside ng-show.
<img ng-src="{{imgProcessingPath}}" ng-show="imgProcessingShow">

ng model not updating in radio button group with ng-repeat

I am displaying radio buttons based on search result using ng-repeat.
if i select any radio button, its color will change.
actual issue is, when i do second search, list will replace with new list.
but when i select any radion button its color not changed.
View:
<div class="col-md-4 col-sm-6 nl_item" ng-repeat="nomineeMgr in nomineesMgrList track by $index">
<div class="checkbox" ng-class="{'blue1': nomineeMgr.SystemUserId===nomManagerSelectedId}">
<label class="radio-inline">
<input type="radio" value="" name="selectedNomineeMgr{{$index}}"
ng-model="$parent.nomManagerSelectedId" ng-value="{{nomineeMgr.SystemUserId}}" />{{nomineeMgr.SystemUserId===nomManagerSelectedId?"SELECTED"}}
</label>
</div>
</div>
angular controller:
$scope.nomManagerSelectedId = 0;
$scope.searchNomineesManager = function (isValid) {
if (!isValid)
return;
var result = pepsicoService.getPepsicoEmployees($rootScope.UrlProvider.getPepsicoSearchEmployeesUrl, null, $scope.searchNomination.searchNomineMgr).then(function (response) {
if (response.data.success) {
$scope.nomineesMgrList = [];
$scope.nomManagerSelectedId = 0;
$scope.nomineesMgrList = response.data.EmployeeList;
}
else {
$rootScope.showFailureMsg(response.data.errorMessage);
}
});
};
any one have an idea to solve this issue?

ng-show ng-hide using controller

I am trying to edit a field and convert label into text field on click of a button and change it back to label on keypress event (ng-keypress).
I am changing the ng-show variable through controller but it is not working.
HTML:
<div ng-app>
<div ng-controller="showCrtl">
<form>
<label ng-hide="editMode" >{{field}}</label>
<input ng-show="editMode" ng-keypress="changemode($event) " ng-model="field" >
<span class="pull-right" >
<button ng-click="editMode=true" class="btn-lg btn-warning" >edit </button> </span>
</form>
</div>
</div>
JS:
function showCrtl($scope){
$scope.field="Chanel";
$scope.changemode=function(event){
if(event.charCode==13){
$scope.editMode = false;
}
}
}
My updated JS-Fiddle link: http://jsfiddle.net/8Yz7S/281/
Use ng-blur instead of ng-keypress,
<input ng-show="editMode" ng-blur="changemode() " >
DEMO
EDIT:
Use the following directive to handle the enter key event,
app.directive('ngEnter', function() {
return function(scope, element, attrs) {
element.bind("keydown keypress", function(event) {
if (event.which === 13) {
scope.$apply(function() {
scope.$eval(attrs.ngEnter);
});
event.preventDefault();
}
});
};
});
DEMO
Can you try the below solution.
<input ng-show="editMode" ng-keypress="changemode($event) " >
Added interval to update the view
function showCrtl($scope, $timeout){
$scope.changemode=function(event){
if(event.charCode==13){
$timeout(function() {
$scope.editMode = false;
}, 500);
}
}
}
http://jsfiddle.net/gsjsfiddle/hcu5mkhm/3/
The reason its not working for you is that, you are not preventing the default behavior of Enter key. So After changemode function is executed and editmode is set to false, click event of Edit button is also getting executed, setting editmode back to true.
All you need to do is call event.preventDefault(); as shown below:
function showCrtl($scope){
$scope.field="Chanel";
$scope.changemode=function(event){
if(event.charCode==13){
$scope.editMode = false;
event.preventDefault(); // <<<<<<<
}
}
}
To verify this behavior you can check this fiddle: http://jsfiddle.net/vnathalye/8Yz7S/301/
Try it after commenting the line event.preventDefault(); and check the console.
You want to obfuscate as much logic as possible from the view. So as he suggested, use
<input ng-show="editMode" ng-keypress="changemode($event)">
Then, do all your logic inside the changemode() function:
$scope.changemode = function(evt) {
$timeout(function() {$scope.editMode = false;},100);
}
http://jsfiddle.net/8Yz7S/293/

How to change the placeholder in the textbox by changing checkbox using angular?

I am trying to change the placeholder in the textbox by changing the checkbox using angular. I tried fiddle http://jsfiddle.net/d9uc1dxc/5/.
I have the following code:
**SCRIPT**
function TController($scope) {
$scope.placeholder="%";
$scope.formData = {};
$scope.radioChecked = function ()
{
$scope.placeholder="%";
$scope.apply();
}
$scope.radiounChecked = function ()
{
$scope.placeholder="Count";
$scope.apply();
}
}
What I am trying to achieve is (1) if I change to "%" then the textbox placeholder and (2) if I change to "Count" then the textbox placeholder should change to "Count".
Someone please help me in this issue.
please try to use this code
this is the script file
var app = angular.module("TestApp", []);
function TController($scope) {
$scope.placeholder="%";
$scope.formData = {};
$scope.radioChecked = function ()
{
if($scope.placeholder == "%"){
$scope.placeholder = "Count";
}else{
$scope.placeholder = "%";
}
}
}
and here is the html file
<body ng-app="TestApp" ng-controller="TController">
<div class="flw vreq2">
<label>Voting require2</label>
<div class="oneline">
<div class="onoffswitches">
<input type="checkbox" name="onoffswitch" ng-click="radioChecked()" class="onoffswitch-checkbox" id="myonoffswitch7"/>
<label class="onoffswitch-label" for="myonoffswitch7">
<span class="onoffswitch-inner"></span>
<span class="onoffswitch-switch"></span>
</label>
</div>
</div>
</div>
<div class="per1">
<input type="text" placeholder="{{placeholder}}" ng-model="formData.textInput" />
</div>
</body>
hope you find this useful
you dont need the two functions for checked & unchecked
use ng-change in check box as and add a model to checkbox // model name = onoffswitch
<input type="checkbox" ng-model="onoffswitch" ng-change="radioChecked()" ...
when checkbox checked, onoffswitch model value will be true otherwise false
when changing the checkbox radioChecked() function will invoke
$scope.radioChecked = function ()
{
if($scope.onoffswitch) {
$scope.placeholder="%";
} else {
$scope.placeholder="Count";
}
}
see the demo Plunker

Validate Input fields only on submit angularjs

I am trying to show a Validation Summary, a Div on top of the page with all the Validation error messages in angularjs, on form submit.
I am using the below logic to show/ hide the top div with validation messages,
<div class="alert alert-error" ng-show="submitted && myForm.$invalid">
</div>
I am setting the variable submitted to true on save button click. It's working Okay the first time, but after the first submission, if enter the value for the input field(required field) and clear it, it's kicking off the validation(the top div shows).
Is there a way to show the validation div, only on the submit of the form and not when the user clears the input field ?
UPDATE
$scope.save = function (myForm) {
$scope.submitted = true;
if (myForm.$invalid) {
return;
}
$scope.submitted = false;
}
Thanks !
Hmm one way to do it would be to watch the FormController's property $dirty and $setPristine() method, and use a variable hasError to show the error or not.
See this plunker as an example
JAVASCRIPT
controller('AppController', ['$scope', function($scope) {
$scope.hasError = false;
$scope.$watch('theForm.$dirty', function() {
$scope.hasError = false;
$scope.theForm.$setPristine();
});
$scope.save = function() {
$scope.hasError = $scope.theForm.$invalid;
if($scope.hasError) {
// perform error routine
} else {
// perform save routine
}
};
}]);
HTML
<body ng-controller="AppController">
<div class="error" ng-show="hasError">This is an error</div>
<form name="theForm" ng-submit="save()" novalidate>
<input type="text" name="text1" ng-model="text1" required>
<input type="text" name="text2" ng-model="text2" required>
<button type="submit">Submit</button>
</form>
</body>
Make sure you are setting submitted to false upon display of your validation div else it'll show up after each additional validation call.
$scope.save = function (myForm) {
$scope.submitted = true;
if (myForm.$invalid) {
$scope.submitted = false;
return;
}
}

Resources