I have an Angular directive that includes a form with a datepicker. I am trying to create a function to keep the button click from calling submit on the form. I can not determine where to find the .opened attribute. When I was performing the open with ng-click everything worked. Below is sample code.
HTML:
<form ng-submit="editAlert.$valid && vm.handleSubmit()" name="editAlert" id="editAlert" class="buffer-bottom" novalidate>
<div class="form-group">
<label class="control-label" for="effectiveDate">Effective Date</label>
<div class="input-group">
<input type="text" class="form-control" uib-datepicker-popup="MM-dd-yyyy" is-open="dpEffectiveDate.opened" ng-model="alertMsg.effectiveDateFmt" name="effectiveDate" id="effectiveDate">
<span class="input-group-btn">
<button class="btn btn-default" ng-click="vm.openDateHandler($event)"><i class="fa fa-calendar"></i></button>
</span>
</div>
<p class="help-block" ng-show="editAlert.effectiveDate.$error.endBeforeStart">Effective date must be before expire date.</p>
</div>
</form>
I also tried passing dpEffectiveDate into the openDateHandler function.
Below is my Typescript function
openDateHandler = ($event) => {
$event.preventDefault();
//dpDate.opened = !dpDate.opened;
}
How do I access the .opened attribute?
If all you are trying to do is prevent the submit behaviour, simply add 'type="button"' to your button element.
<button type="button" class="btn btn-default" ng-click="vm.openDateHandler($event)">
By default, a button inside a form will act as a submit button unless it's given a different type.
You shouldnt need to mess with the opened state of a date picker to prevent a form submitting.
Related
When I used Datetime picker for a single input, it worked fine.
But when I used ng-repeat and the ng-model values didn't show up inside the input box though ng-model values were saved properly.
Please check the following code:
<div class="input-group">
<span class="error-input"
ng-if="vm.editForm['employeeOnProjectStartDate'+$index].$error.required && vm.isSavedBefore">
This field is required
</span>
<input type="text" class="form-control" required
name="employeeOnProjectStartDate{{$index}}"
enable-time="false" datetime-picker="dd-MMM-yyyy" ng-model="employee.employeeOnProjectStartDate"
is-open="vm.datePickerOpenStatus.employeeOnProjectStartDate[$index]" />
<span class="input-group-btn">
<button class="btn btn-default" ng-click="vm.openCalendar('employeeOnProjectStartDate',$index)">
<i class="glyphicon glyphicon-calendar"></i>
</button>
</span>
</div>
the solution is to add ng-value as well.
ng-value="employee.employeeOnProjectStartDate | date:'dd-MMM-yyyy'" solved the issue.
I currently have the following html which consists of 3 buttons (1 hidden). I have a button which triggers a hidden button which is used to select a file to upload and I have a third button which is used to call upload and send the data to the server. I would like to change this so that after selecting a file, the file is automatically uploaded. I am having issues triggering the upload button when a file is selected (and then I will hide the upload button once I have the automatic upload functionality working). I have tried using the onchange event to accomplish this but with no success
<div class="col-xs-12" style="margin-top:250px">
<div class="col-xs-4" align="center" style="padding-left:290px">
<button class="btn btn-default" ng-click="newProject()">New Project</button>
</div>
<form class="col-xs-4" align="center"
ng-controller="CsvImportController" style="display: inline-block;padding-left:200px" id="csvForm">
<a href="#" class="btn btn-default btn-file"
onclick="document.getElementById('fileBrowser').click(); return false;">Select
CSV File</a>
<button id="submitCsv" class="btn btn-default" ng-click="upload()">Upload</button>
</form>
<div class="col-xs-4" align="center" style="padding-left:110px">
<a class="btn btn-default">Browse
Projects</a>
</div>
</div>
<br>
<br>
<input id="fileBrowser" style="visibility: hidden;"
class="btn btn-default btn-file" type="file" id="file" file-input="files" onchange="document.getElementById('submitCsv').click(); return false;"/>
See this answer for some libraries that will handle all the upload functionality for you as well as provide some other nice features.
onchange will not work on <input type="file"/> as it will not register a change when you select a file. ng-click will not work also as it will trigger when the input is first clicked, not when a file is selected.
You also can't apply any styles to an <input type="file" /> either, so I get what you're trying to do here, but <input type="file" /> does not offer much functionality which is really unfortunate.
You also have 2 ids defined on your hidden input. Make sure there is only one id so that you can call it correctly.
I want to open my calendar from both 1. on clicking the textbox and 2. on clicking the calendar Icon
<div class="form-group">
<div class='input-group'>
<input type='text' class="form-control" datepicker-popup
ng-model="model.start" is-open="opened.start" id='start'
ng-click="opened.start = !opened.start" />
<span class="input-group-addon" for='start' ng-click="opened.start = !opened.start">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
And controller code:
app.controller('dateCtrl', ['$scope', function ($scope) {
$scope.model = {
start: new Date('12/31/2014'),
end: new Date()
};
$scope.opened ={
start: false,
end: false
};
}]);
when I am clicking on the textbox it is working, but when I am clicking on calendar icon, nothing happening.
See the plunker.
Use labels instead of spans, they are designed for this purpose. Just make sure to set up correct for and corresponding id attributes:
<div class='input-group'>
<input type='text' class="form-control" datepicker-popup
ng-model="model.start" is-open="opened.start" id='start'
ng-click="opened.start = !opened.start" />
<label class="input-group-addon" for='start'>
<span class="glyphicon glyphicon-calendar"></span>
</label>
</div>
Clicking on HTMLLabelElement generates click event on the related input field, so in this case you get intended behaviour.
Demo: http://plnkr.co/edit/xGPVMPPTOlzOhFIVdhG4?p=preview
UPD. Since you need toggle functionality, then in this case you indeed need javascript solution. Here we go, you need to prevent event bubbling, because Angular listens for events on the document level and closed calendar immediately:
<span class="input-group-addon" ng-click="opened.start = !opened.start; $event.stopPropagation()">
<span class="glyphicon glyphicon-calendar"></span>
</span>
Demo: http://plnkr.co/edit/CmJK1M0icGpKvd38S9mK?p=preview
I have a form which is dynamically generating the fields, I am using ng-form for this. The form has an ng-repeat for input fields and the main form has a 'Save' button.
i.e when one of the input fields is dirty and has content, the save button should be enabled and should be disabled otherwise.
Here is my HTML Code :
<form class="form-horizontal" name="$parent.fSForm" novalidate ">
<fieldset class="form-group pl-sm" ng-repeat="fs in list track by $index">
<ng-form name="innerForm">
<div class="col-md-5">
<input type="text" class="form-control" autocomplete="off" name="fsl" ng-change="fileChanged()" ng-model="fs.path" placeholder="Add new here">
</div>
</ng-form>
</fieldset>
<a class="prop-toggle ng-binding" href="" ng-click="addNew()">Add More</a>
</form>
</div>
<div class="footer">
<span>
<button type="button" class="btn" ng-click="close()">Cancel</button>
</span>
<span>
<button type="submit" class="btn btn-primary" ng-click="save()" ng-disabled="fSForm.$error.fooname">Save</button>
</span>
</div>
So for my save button to be disabled when the form loads initially how should I go about validating that with an ng-form present? The save should be enabled when one of the input fields has some text in it. not sure how we can handle ng-disabled with ng-form within a form save button
http://plnkr.co/edit/I5JkHiNnwmklHAKh3ag9?p=preview
<form ng-model-options="{ updateOn: 'submit' }">
<input type="text" class="form-control" ng-model="activeEditorData.startDate" data-date-format="yyyy-MM-dd" data-date-type="number" data-min-date="02/10/86" data-max-date="today" data-autoclose="1" name="date2" bs-datepicker>
<button type="button" class="btn btn-default" ng-click="$hide()">Cancel</button>
<button type="submit" class="btn btn-primary" ng-click="$hide()">Save changes</button>
</form>
I am having problems with ng-model-options and the angular-strap datepicker. The actual $scope.date is updated exactly as expected when pressing save, but the datepicker input date does not update as it should when changing dates. Is there some way to force the datepicker to update it's value or something?
If you remove the ng-model-options attribute it works as expected, but that removes the possibility to roll back all the data upon the cancel press, which is problematical for a bigger form.
So certain directives just doesn't seem to like two-way bindings outside the current parent scope, so I simply removed the ng-model-options and made a copy of the object.
Controller:
/**
* ng-model-options won't work with certain directives,
* so this is needed to replicate its behaviour.
*/
$scope.dataCopy = angular.copy($scope.activeEditorData);
$scope.save = function(hide) {
angular.forEach($scope.dataCopy, function(value, key) {
$scope.activeEditorData[key] = value;
});
$scope.exit(hide);
};
Html:
<form>
<input type="text" class="form-control" ng-model="dataCopy.startDate" data-date-format="yyyy-MM-dd" data-date-type="number" data-min-date="02/10/86" data-max-date="today" data-autoclose="1" name="date2" bs-datepicker>
<button type="submit" class="btn btn-primary" ng-click="save($hide)">Save changes</button>
</form>