Issue which showing the image - angularjs

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">

Related

onclick button get the value of selected (checkbox) value

<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")
})
}

ngDisabled validation not working

I'm trying to add a single disabled="disabled" to a button, to block the element while the it is creating a server requests, however, for some reason the documentations information is not working for me, so my case is:
HTML:
<button ng-disabled="isDisable" ng-click="submit()">Submit</button>
JS (into my controller):
$scope.isDisable = false;
$scope.submit = function() {
$scope.isDisable = true;
//$http request....
something.sucess(function(data) {
$scope.isDisable = false;
};
};
for some reason my logic is not working.
Any ideas?
You don't need the {{}} as it's not an expression.
<button ng-disabled="isDisable" ng-click="submit()">Submit</button>
EDIT
If this is a form then on the form tag do ..
<form name="aForm" novalidate ng-submit="submit()">
then submit button
<button type="submit" ng-disabled="isDisable">Submit</button>

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;
}
}

What is the AngularJS way of handling a modal like this

I know you're not supposed to put your display logic inside of a controller and I'm struggling with the proper AngularJS way to approach this.
I'm presenting forms inside modals. I'm using Zurb Foundation's reveal for the modal.
Markup:
<div class="button" ng-click="modalAddWidget">Add Widget</div>
<div id="modalAddWidget" class="reveal-modal">
<h6>New Widget</h6>
<form>
<fieldset>
<legend>Widget Name</legend>
<input type="text" ng-model="ui.add_widget_value" />
</fieldset>
<div class="small button right" ng-click="addWidget()">Add Widget</div>
<div class="small button right secondary" ng-click="addWidgetCancel()">Cancel</div>
</form>
</div>
Controller:
...
$scope.modalAddWidget = function() {
$("#modalAddWidget").reveal();
}
$scope.addWidget = function() {
$scope.myobject.widgets.push({"name": $scope.ui.add_widget_value});
$scope.ui.add_widget_value = '';
$('#modalAddWidget').trigger('reveal:close');
}
$scope.addBudgetCancel = function() {
$scope.ui.add_widget_value = '';
$('#modalAddWidget').trigger('reveal:close');
}
...
Note: $scope.ui is an object I am using to store UI values that shouldn't be bound to my object until the user actually clicks "Add Widget"
$scope.myobj is where my data is stored.
Foundation's $("#modalAddWidget").reveal(); function presents the modal overlay.
Since I shouldn't be putting my display code inside the controller, what is the proper way to approach this?
You don't want to manipulate the DOM (or even reference it) from your controllers.
A directive is best here.
app.directive('revealModal', function (){
return function(scope, elem, attrs) {
scope.$watch(attrs.revealModal, function(val) {
if(val) {
elem.trigger('reveal:open');
} else {
elem.trigger('reveal:close');
}
});
elem.reveal();
}
});
then in your controller:
$scope.modalAddWidget = function (){
$scope.ui = { add_widget_value: '' };
$scope.showModal = true;
};
$scope.addWidget = function (){
$scope.myobject.widgets.push({"name": $scope.ui.add_widget_value});
$scope.ui.add_widget_value = '';
$scope.showModal = true;
};
And in your HTML
<div class="button" ng-click="modalAddWidget()">Add Widget</div>
<div id="modalAddWidget" class="reveal-modal" reveal-modal="showModal">
<h6>New Widget</h6>
<form name="addWidgetForm" ng-submit="addWidget()">
<fieldset>
<legend>Widget Name</legend>
<input type="text" name="widgetValue" ng-model="ui.add_widget_value" required />
<span ng-show="addWidgetForm.widgetValue.$error.required">required</span>
</fieldset>
<button type="submit" class="small button right">Add Widget</button>
<div class="small button right secondary" ng-click="showModal = false;">Cancel</div>
</form>
</div>
Basically you'd set a boolean in your scope to show and hide your modal. (I'm not sure of reveal modal's open/close mechanism, so I'm guessing in my code above).
ALSO: I went through the effort of adding some validation in there.

Resources