AngularJS- how to hide elements using ng-hide? - html

I have an app that has been written in AngularJS, and am currently trying to add the functionality to hide the headings of some widgets displayed on one of the web pages when the user selects a checkbox to indicate that they should be hidden.
At the moment, I have a page that displays a number of widgets- on the 'heading' of each widget, there is a 'Settings' button. When the user clicks the Settings button, a dialog box opens up on top of the current page (i.e. the user does not navigate to another page- the URL does not change at all). That dialog box contains a form with a number of input fields- one of which is a checkbox that I want to use to 'hide' the headings of all of the widgets on the webpage.
I have been following the example at: https://docs.angularjs.org/api/ng/directive/ngHide to try and do this, but can't quite seem to get it working...
I have added the ng-hide attribute to my HTML element, as in the example:
<div data-ng-if="widget.name === 'tag-box'" ng-hide="hideWidgetHeading"> <!-- class="ng-hide"-->
<div class="divider"></div>
...
<div class="divider"></div>
<div class="row ui-checkbox-row">
<label class="col-sm-4 col-xs-6" data-i18n="Hide widget heading:"></label>
<div class="col-sm-8 col-xs-6">
<label class="ui-checkbox">
<input type="checkbox" name="noWidgetHeading" id="noWidgetHeading" ng-true-value= "'YES'" ng-false-value= "'NO'" ng-change="hideWidgetHeading()" ng-click="hideWidgetHeading()" ng-checked="hideWidgetHeading" ng-model="viewModel.hideWidgetHeading">
<!-- ng-model="viewModel.hideWidgetHeading" -->
<span></span>
</label>
</div>
</div>
</div>
I defined the hideWidgetHeading() function in the ctrl.js file as follows:
function hideWidgetHeading(){
if($scope.widgetHeadingCheckbox==false) {
$scope.$watch('noWidgetHeading', function() {
$scope.hideWidgetHeading = true;
console.log("Value of hideWidgetHeading: ", $scope.hideWidgetHeading);
});
return true;
} else {
console.log("hideWidgetHeading() else called (Widget/ctrl.js line 440) ");
$scope.$watch('noWidgetHeading', function() {
$scope.hideWidgetHeading = false; //document.getElementById('noWidgetHeading');
});
return false;
}
if($scope.hideWidgetHeading) {
console.log("hideWidgetHeading is true- hide the widget heading: ");
}
return $scope.hideWidgetHeading;
}
and I have added the following CSS to my widgets.scss file:
.animate-show-hide.ng-hide {
opacity: 0;
}
.animate-show-hide.ng-hide-add,
.animate-show-hide.ng-hide-remove {
transition: all linear 0.5s;
}
.umw-tag-box {
opacity: 1;
}
When I load my page as it is presently, when I click the Settings button, the dialog opens up. If I then check the 'Hide widget heading' checkbox, and click Submit, the debug that I have added displays the following in the console:
Value of hideWidgetHeading: true
which tells me that the code inside the $scope.$watch(...){...} function is running.
However, if I click the Settings button, and then either don't check the 'Hide widget heading' checkbox, or check it and uncheck it again, and then click Submit, I get the same true value displayed in the console debug, which indicates that the code inside the $scope.$watch(...){...} function is running regardless of whether the 'watched' element changes or not.
Questions
How can I ensure that the code inside the $scope.$watch(...){...} only runs when the 'watched' element (i.e. the checkbox) has its value changed?
How do I actually 'call' the CSS that I've added to hide the HTML elements on the particular HTML that I want to hide? Or how do I make that CSS 'apply' to the HTML elements that I want to hide?
Edit
I changed the HTML for the checkbox to:
<input type="checkbox" ng-model="checked" name="noWidgetHeading" id="noWidgetHeading">
as suggested, and when I now browse to my page, and open the dialog, it displays the widget as I expect:
When I click the 'Settings' button on the widget, the 'Configure Item' dialog opens up on top of the page:
But when I select the 'Hide widget heading' checkbox, it actually hides the 'Tag' label & input box from the dialog:
The element I want to hide is actually displayed on the page from which the dialog box is opened, not on the dialog box itself... but I can't seem to work out how I can hide that using a control on the dialog... Any suggestions?

So, this should work, it is even the first example shown in their docs, with your code it will be:
<div data-ng-if="widget.name === 'tag-box'" ng-hide="viewModel.hideWidgetHeading"> <!-- class="ng-hide"-->
<div class="divider"></div>
...
<div class="divider"></div>
<div class="row ui-checkbox-row">
<label class="col-sm-4 col-xs-6" data-i18n="Hide widget heading:"></label>
<div class="col-sm-8 col-xs-6">
<label class="ui-checkbox">
<input type="checkbox" name="noWidgetHeading" id="noWidgetHeading" ng-true-value= "'YES'" ng-false-value= "'NO'" ng-change="logWidgetHeading()" ng-click="logWidgetHeading()" ng-checked="viewModel.hideWidgetHeading" ng-model="viewModel.hideWidgetHeading">
<!-- ng-model="viewModel.hideWidgetHeading" -->
<span></span>
</label>
</div>
</div>
</div>
you can keep functions on the events if you wan to log values, but there is no need for hiding. The ng-model directive will update your value and the ng-hide should follow.
function logWidgetHeading(){
console.log("Value of hideWidgetHeading: ", $scope.hideWidgetHeading);
}
What i was saying about function or var: in some cases, i used to have values from the scope that were not updated, and the solution was to introduce a function to be called to retreive the value. It is what i thought you were trying because ng-hide="hideWidgetHeading" shows the same name as your function: function hideWidgetHeading(){, that's why i first said that the round brackets were missing. So another version would be something like this (without ng-model on purpose, to show an alternate way to modify stuff with your events):
<div data-ng-if="widget.name === 'tag-box'" ng-hide="getHideWidgetHeading()"> <!-- class="ng-hide"-->
<div class="divider"></div>
...
<div class="divider"></div>
<div class="row ui-checkbox-row">
<label class="col-sm-4 col-xs-6" data-i18n="Hide widget heading:"></label>
<div class="col-sm-8 col-xs-6">
<label class="ui-checkbox">
<input type="checkbox" name="noWidgetHeading" id="noWidgetHeading" ng-true-value= "'YES'" ng-false-value= "'NO'" ng-change="toggleWidgetHeading()" ng-checked="isHiddenWidgetHeading">
<!-- ng-model="viewModel.hideWidgetHeading" -->
<span></span>
</label>
</div>
</div>
</div>
and the js:
//initialisation to the default value:
$scope.isHiddenWidgetHeading = false;
//function that toggles the value:
$scope.toggleWidgetHeading = function(){
$scope.isHiddenWidgetHeading = !$scope.isHiddenWidgetHeading;
};
//function to retreive the value:
$scope.getHideWidgetHeading = function(){
return $scope.isHiddenWidgetHeading;
};
Sorry i was a bit quick in naming vars and such, but you should get what you need here..

Related

Electron Get file path from form and display as text

I am creating a simple program in Electron. The program has the option of running several separate functions based on what the user needs. All functions require a file to be inputted and a save location for the resulting output file. This is done using a form. I would like to have it that once the user inputs the locations it is displayed in a div beside the input buttons. Is there a way to do this within electron?
code:
<!-- File Input Section -->
<div class = "individual-input-container-2">
<div class="input-container" >
<div class = "inner-input-container">
<input type="file" id="file-input" class = "input-top" >
<p class = "input-desc-file">File</p>
</div>
<div>
</div>
</div>
<div class="input-container">
<div class = "inner-input-container">
<input type="file" webkitdirectory id="save-input"class = "input-bottom">
<p class = "input-desc-save">Save Location</p>
</div>
</div>
</div>
Here is photo of what I am building
I did something similar a while back and mine looks like this:
HTML:
<button id="choosePath">Choose Folder</button>
JS:
const { dialog } = require('electron').remote;
document.querySelector('#choosePath').addEventListener('click', (e) => {
dialog.showOpenDialog({
title:"Select Directory",
properties: ["openDirectory"]
}, (folderPaths) => {
// folderPaths is an array that contains all the selected paths
if(folderPaths === undefined){
return;
} else {
// Do something with the folderPaths variable
}
});
});
It's basically just an ordinary button opening a Dialog Window where you can select a path.
If you did select one the full Path is passed to a callback function where you can use it to do whatever you have to do with it.
You can try Electron's dialog.showSaveDialog ()/dialog.showSaveDialogSync () functions. They return a Promise<string> or a string representing the file/folder which was selected by the user, respectively. Then you can easily display in your <div>.

Angular6 *ngIf doesnt load div section the first time the search button is clicked, instead loads on 2nd click

Working on displaying and hiding a section using *ngIf, the div doesnt get activated on the first click of the button, but gets activated on the second.
Scenario -
On entering a frame number and click of the search button a http call is made, the number received is checked against the status if it is true or false, if false the section should remain closed, meaning the data entered is wrong, if correct then data section will open and be patched with a few values.
The below is the code at my component, where i am checking if the status from my response is true or false and displays notification once my status is fault.
Issue- though the condition is met of being true, my ngif section doesnt display, instead displays on the second click
onSubmitVehicleDetails() {
console.log(this.vehdetails.status)
console.log(this.fetch_vehicle);
let formValue1: any = {
chassis_no: this.vehicleDetailsForm.value.chassis_no,
}
this.policyService.getVehicleNumber(formValue1)
.pipe()
.subscribe(vehdetails => {
this.displayModalPolicy(vehdetails),
console.log(this.vehdetails.status)
if(this.vehdetails.status === false){
this.fetch_vehicle = false;
console.log(this.fetch_vehicle)
}
else {
this.fetch_vehicle = true;
console.log(this.fetch_vehicle)
}
console.log('vehdetails: ', vehdetails); //
(error: any) => this.errorMessage = <any>error;
console.log(this.vehdetails.status)
}
)
}
displayModalPolicy(vehdetails): void {
this.vehdetails = vehdetails;
this.vehicleDetailsForm.patchValue({
model_name: vehdetails.data.model_name,
v_fuel: vehdetails.data.v_fuel,
v_yom: vehdetails.data.v_yom,
v_cc: vehdetails.data.v_cc,
variant_name: vehdetails.data.variant_name,
exshow_price: vehdetails.data.exshow_price,
engine_no: vehdetails.data.engine_no,
});
console.log(vehdetails);
// the data on the form
console.log(this.vehdetails.status)
console.log(this.fetch_vehicle)
}
This is the HTML
<button mat-raised-button color="primary" type="button"
(click)="[onSubmitVehicleDetails()]">Search</button>
<p>
{{fetch_vehicle}}
</p>
</div>
</div>
<div *ngIf="fetch_vehicle">
<div class="form-group m-form__group row">
<div class="col-lg-6">
<img src="assets/app/media/img/logos/logo-1.png">
<label class="col-lg-4 col-form-label">
<h4>Vehicle Details</h4>
</label>
</div>

How to reset check status in html

I have popup in HTML
<div id="term_flags" class="term_flags">
<div class="modal-users-content flagsContent">
<div class="modal-users-header">
<span class="close" ng-click="closeFlagsPopup()">×</span>
<a> Check terminal flags </a>
</div>
<div class="modal-flags-body">
<div class="checkBoxes">
<div class="checkerDiv">
<input type="checkbox" ng-model='reservedFlag' ng-click='changeReservedStatus(reservedFlag)' value="flag" ng-checked="reservedFlag"> Reserved
</div>
<div class="checkerDiv">
<input type="checkbox" ng-model='seasonFlag' ng-click='changeSeasonStatus(seasonFlag)' value="flag" ng-checked="seasonFlag"> Season
</div>
<div class="checkerDiv">
<input type="checkbox" ng-model='networkFlag' ng-click='changeNetworkStatus(networkFlag)' value="flag" ng-checked="networkFlag"> Network
</div>
</div>
<div class="saveFlags">
<button class="button button6" name="changeFlags" value="Change Flags" type="submit" ng-click="saveFlags(item.terminalId)"> Save <p> </button>
</div>
</div>
<div class="modal-footer"></div>
</div>
</div>
this div's display is none in the beginning. but some ng-click is called from outside of this div and display is changed from none to block and initializes checkbox statuses in this angular function
$scope.changeFlagStatus = function(item)
{
$scope.reservedFlag=(item.reservedFlag=='T')?true:false;
$scope.networkFlag=(item.networkFlag=='T')?true:false;
$scope.seasonFlag=(item.seasonFlag=='T')?true:false;
document.getElementById('term_flags').style.display = "block";
}
everything is okay , but when i click on reservedFlag changeReservedStatus(reservedFlag) method was called and change reservedFlag's checked status
$scope.changeReservedStatus = function(item) {
$scope.reservedForSave=item;
}
I saved this status in other variable and close my popup windows document.getElementById('term_flags').style.display=none
when i open this popup window again my function changeFlagStatus(item) is called again and initializes my variables for checkbox correctly but my checkbox are incorrect checked .
In example when i opened my popup window first time my variables after initialize were
$scope.reservedFlag=true;
$scope.networkFlag=false;
$scope.seasonFlag=true;
and my checkbox statuses were
reservedFlag = checked
networkFlag = unchecked
seasonFlag = checked
then i clicked on reservedFlag and changed his status from checked to unchecked and close my popup windows.
then i opened it second time and changeFlagStatus(item) method is called again to initialize my variables again for checkbox statuses
and i want to get
reservedFlag = checked
networkFlag = unchecked
seasonFlag = checked
again, but result is
reservedFlag = unchecked
networkFlag = unchecked
seasonFlag = checked
How can i get it ?
Angularjs won't work pretty good with $scope's properties for primitive variables when used again and again.
I would recommend you to declare an object to scope and set these flags as properties to this object.
$scope.flags = {};
$scope.changeFlagStatus = function(item)
{
$scope.flags.reservedFlag=(item.reservedFlag=='T')?true:false;
$scope.flags.networkFlag=(item.networkFlag=='T')?true:false;
$scope.flags.seasonFlag=(item.seasonFlag=='T')?true:false;
document.getElementById('term_flags').style.display = "block";
}
and the html part as
<input type="checkbox" ng-model='flags.reservedFlag' ng-change='changeReservedStatus()' value="flag" ng-checked="flags.reservedFlag"> Reserved
Instead of ng-click, I would recommend to use ng-change because that is the event to be used with checkbox. If you use ng-change, the injection of the model as a parameter can be avoided which helps in utilizing angularjs's feature of 2-way binding.
You can add ng-true-value and ng-false-value to the checkbox with true and false to make it more easier to handle instead of ng-value.
I would write the html part like this.
<input type="checkbox" ng-model='flags.reservedFlag' ng-change='changeReservedStatus()' ng-true-value="true" ng-false-value="false"> Reserved
Hope this will fix the issue.

AngularJS- document.getElementById() - Why does element return null?

I have a checkbox on a dialog box that is opened when clicking a button in an AngularJS widget in my HTML form:
<form class="m-body" role="form" data-ng-submit="preview($event)" novalidate>
...
<div data-ng-if="widget.name === 'display-box'" ng-dige="hideWidgetHeading()">
<div class="divider"></div>
<div class="row">
...
</div>
...
<div class="row ui-checkbox-row">
<label class="col-sm-4 col-xs-6" data-i18n="Hide widget heading:"></label>
<div class="col-sm-8 col-xs-6">
<label class="ui-checkbox">
<input type="checkbox" name="noWidgetHeading" ng-true-value= true ng-false-value= false ngChange="hideWidgetHeading()" ng-click="hideWidgetHeading()" >
<span></span>
</label>
</div>
</div>
</div>
...
In the ctrl.js file, I have the following function:
angular.module('app.widget').run(function($templateCache){
...
}).controller('WidgetPickerCtrl', function($scope, $timeout, fxTag, gPresets, appWidget){
...
$scope.widget = undefined;
...
$scope.checkboxModel = {
value1 : true,
value2 : false
};
...
var widgetHeadingCheckbox = document.getElementById("noWidgetHeading");
console.log(widgetHeadingCheckbox);
$scope.$watch('widgetHeadingCheckbox', function(){
console.log("Value of checkbox has changed: ", widgetHeadingCheckbox);
});
function hideWidgetHeading(){
if(widgetHeadingCheckbox){
console.log("Value of widgetHeadingCheckbox in 'if': ", widgetHeadingCheckbox);
return widgetHeadingCheckbox;
} else {
console.log("Value of widgetHeadingCheckbox in 'else: ", widgetHeadingCheckbox);
return widgetHeadingCheckbox;
}
}
Currently, when I load the page, and click the button to open the dialog box, the checkbox is displayed on the dialog box unchecked. When I click it, it shows that it is checked, and if I click it again, it becomes unchecked again, as you would expect.
I am trying to use the hideWidgetHeading() JS function above to perform a different action when the dialog box 'Submit' button is pressed, depending on whether the checkbox is selected or not.
At the moment, if I select the checkbox (i.e. it's checked- its value should be true), and click the 'Submit' button on the form, I get the following output in my console:
null
Value of checkbox has changed: null
Value of widgetHeadingCheckbox in else: null
I also get the same output in the console if I click the submit button when the checkbox is not checked...
Why is the value of my checkbox always null... I would expect it to be either true or false, but never null... Do I need to initialise it somewhere? I thought I had already done that in the HTML, when setting its ng-true-value & ng-false-value...
Edit
Following what dcrux has said in their answer, I have changed the HTML for the dialog box on which the checkbox is displayed to:
<div data-ng-if="widget.name === 'umw-tag-box'" ng-hide="hideWidgetHeading">
...
<div class="row ui-checkbox-row">
<label class="col-sm-4 col-xs-6" data-i18n="Hide widget heading:"></label>
<div class="col-sm-8 col-xs-6">
<label class="ui-checkbox">
<input type="checkbox" ... ng-model="hideWidgetHeading">
<span></span>
</label>
</div>
</div>
</div>
When I click the 'settings' button on the widget, the dialog box opens:
However, when I then check the 'Hide widget heading' checkbox, what it actually hides is stuff from the dialog, rather than from the widget:
What I want it to hide is the title bar of the widget itself, not anything from the dialog box:
How can I get it to hide something from the widget, rather than from the dialog box?
use ng-model in your input tag
<input type="checkbox" name="noWidgetHeading" ng-true-value= true ng-false-value= false ngChange="hideWidgetHeading()" ng-model="noWidgetHeading" ng-click="hideWidgetHeading()" />
keep ng-model in your input tag ng-model="noWidgetHeading"
And also instead of reading from document
var widgetHeadingCheckbox = document.getElementById("noWidgetHeading");
console.log(widgetHeadingCheckbox);
Try reading from scope
var widgetHeadingCheckbox = $scope.noWidgetHeading;
console.log(widgetHeadingCheckbox)
It's just suggestion try and let me know
You'll want to change that ngChange to ng-change. Also, the clue is in the method name getElementById() - the id is missing, you have the name, but not the id.
I would fist add ng-model="noWidgetHeading" back to the input.
Then change:
var widgetHeadingCheckbox = document.getElementById("noWidgetHeading");
console.log(widgetHeadingCheckbox);
$scope.$watch('widgetHeadingCheckbox', function(){
console.log("Value of checkbox has changed: ", widgetHeadingCheckbox);
});
to just:
$scope.$watch('noWidgetHeading', function(){
console.log("Value of checkbox has changed: ", $scope.noWidgetHeading);
});
Next:
On you HTML- the input does not require ng-true-value or ng-false-value as using 'true' and 'false' is the default. If you not want to use the default ensure you are using an expression so: ng-true-value="'YES'"
Ensure usage of directives is correct: ngChange should be ng-change.
hideWidgetHeading() is not exposed to the scope so can not be called from the front end. Consider binding it to $scope: $scope.hideWidgetHeading = function(){...}
See if these changes fix the problem.

Radio button triggered actions in submit form

I've customized my form, it's a regular order form that has 2 payment options right before the buy now button. I need to have the data going to autoresponder regardless of the payment option chosen...when they choose option 1 (cash), the form will redirect them to the thankyou page...but now that I have 2 radio buttons, how do I separate these events? How do I make them see the thankyou page if they choose option 1 - and redirect them to paypal if they choose the option 2?
<div class="gt-box">
<div class="gt-labelpos">
<label class="gt-label" id="500">How You Pay Mister?</label>
</div>
<div class="gt-inputpos">
<div class="clrB">
<input name="custom_payme" type="radio" class="gt-req gt-valid__required"
checked="checked" value="cash"></input>
<span class="gt-text">Ca$h</span>
</div>
<div class="clrB">
<input name="custom_payme" type="radio" class="gt-req gt-valid__required"
value="PayPal"></input>
<span class="gt-text">PayPal</span>
</div>
</div>
<em class="clearfix clearer"></em>
</div>
Another approach which will help you prevent redirects is on submit button click to evaluate your radio value and redirect from there. You Javascript code would look something like this
$('.submit-btn').click = function(e) {
e.preventDefault();
if ($('.custom_payme').val() === 'cash') {
$.post("Your post url for cash", form_data);
}
else {
$.post("Your post url for Paypal", form_data);
};
};
On you server side when you receive your form's data, you can check the value of the radio button and according to that redirect respectively.