AngularJS 1.5 - same as above address - angularjs

I want to implement a 'same as above' mailing address and permanent address. But then i tick on the checkbox, it does not show that the mailing address and permanent address are the same. help?
controller
$scope.candidateData.MailingAddress = {};
$scope.$watch('mailingSameAsPermanent', function (value) {
if (value) {
$scope.candidateData.Address = $scope.candidateData.MailingAddress;
} else {
$scope.candidateData.Address = angular.copy($scope.candidateData.Address);
}
partial HTML:
<h3>Permanent Address</h3>
<md-input-container class="md-block">
<label>Address</label>
<input name="add" ng-model="candidateData.Address.Address1">
<div ng-messages="CandidateDetails.add.$error">
<div ng-message="required">
Please enter your address
</div>
</div>
</md-input-container>
<md-input-container md-no-float class="md-block">
<input ng-model="candidateData.Address.Address2" placeholder="Address 2">
</md-input-container>
<div layout-gt-sm="row">
<md-input-container class="md-block" flex-gt-sm>
<label>Country</label>
<md-select ng-model="candidateData.Address.Country">
<md-option> <!--ng-repeat="country in countries" value="{{country.Country}}"-->>
{{candidateData.Address.Country}}
</md-option>
</md-select>
</md-input-container>
<md-input-container class="md-block" flex-gt-sm>
<label>Zip Code</label>
<input name="postalCode" ng-model="candidateData.Address.Zip" placeholder="12345"
required ng-pattern="/^[0-9]{5}$/">
<div ng-messages="CandidateDetails.postalCode.$error" role="alert" multiple>
<div ng-message="required" class="my-message">You must supply a zip code.</div>
<div ng-message="pattern" class="my-message">
That doesn't look like a valid postal
code.
</div>
</div>
</div>
<h3>Mailing Address</h3>
<md-checkbox ng-model="mailingSameAsPermanent" >
Tick if your mailing address is the same as your permanent address
</md-checkbox>
<div>
<md-input-container class="md-block">
<label>Address</label>
<input name="add" ng-model="candidateData.MailingAddress.Address1" ng-disabled="mailingSameAsPermanent">
<div ng-messages="CandidateDetails.add.$error">
<div ng-message="required">
Please enter your address
</div>
</div>
</md-input-container>
<md-input-container md-no-float class="md-block">
<input ng-model="candidateData.MailingAddress.Address2" placeholder="Address 2" ng-disabled="mailingSameAsPermanent">
</md-input-container>
<div layout-gt-sm="row">
<md-input-container class="md-block" flex-gt-sm>
<label>Country</label>
<md-select ng-model="candidateData.Address.Country">
<md-option>
<!--ng-repeat="country in countries" value="{{country.Country}}"-->>
{{candidateData.Address.Country}}
</md-option>
</md-select>
</md-input-container>
<md-input-container class="md-block" flex-gt-sm>
<label>Zip Code</label>
<input name="postalCode" ng-model="candidateData.Address.Zip" placeholder="12345"
required ng-pattern="/^[0-9]{5}$/">
<div ng-messages="CandidateDetails.postalCode.$error" role="alert" multiple>
<div ng-message="required" class="my-message">You must supply a zip code.</div>
<div ng-message="pattern" class="my-message">
That doesn't look like a valid postal
code.
</div>
</div>
</div>
</div>

I've implemented the functionality the way you want to achieve but in different approach.
You can check out below link to understand.
https://jsfiddle.net/alpeshprajapati/U3pVM/23941/
$scope.permanent = {address:'helloworld', state: 'gujarat'};
$scope.click = function(){
if($scope.sameasabove){
$scope.mail = angular.copy($scope.permanent);
} else {
$scope.mail = {};
}
};
You need to implement this in your code.

Try this one
<input type="checkbox" ng-model="sameas" ng-click="!sameas?permanentAddress=presentAddress:permanentAddress={}" />

Related

Get all the values of form fields having same ng-model in angularjs

I am new in angularjs and making a dynamic form fields with ng-repeat and all my fields having the same ng-model
My Code:
<form name="dataset" class="md-inline-form" novalidate>
<div ng-repeat="column in columns">
<input type="hidden" name="column_name" ng-model="dataset.columnName" value="{{column}}">
<label for="">Column: <strong>{{column}}</strong></label>
<div layout="column" layout-gt-xs="row">
<md-input-container flex>
<label>Select Type</label>
<md-select name="type" ng-model="dataset.type" required>
<md-option value="string">String</md-option>
<md-option value="numeric">Numeric</md-option>
</md-select>
<div class="errors" ng-messages="dataset.state.$error">
<div ng-message="required">Required</div>
</div>
</md-input-container>
</div>
</div>
<md-button type="submit" ng-click="SavevalidateColumns()" class="md-raised md-accent" aria-label="Submit">Validate Now</md-button>
</form>
Now i just want to get the values of my all dynamically created fields in my controller. Can any one help me regarding this ?
You have to use my code.
<form name="dataset" class="md-inline-form" novalidate>
<div ng-repeat="column in columns">
<input type="hidden" name="column_name" ng-model="dataset.columnName" value="{{column}}">
<label for="">Column: <strong>{{column}}</strong></label>
<div layout="column" layout-gt-xs="row">
<md-input-container flex>
<label>Select Type</label>
<md-select name="type" ng-model="dataset.type" required>
<md-option value="string">String</md-option>
<md-option value="numeric">Numeric</md-option>
</md-select>
<div class="errors" ng-messages="dataset.state.$error">
<div ng-message="required">Required</div>
</div>
</md-input-container>
</div>
</div>
<md-button type="submit" ng-click="SavevalidateColumns(dataset)" class="md-raised md-accent" aria-label="Submit">Validate Now</md-button>
</form>
First You have to pass data in funciton.
Your controller code:
(function () {
'use strict';
angular.module('moduleName', [])
.controller('myCtrl',
function ($scope) {
$scope.SavevalidateColumns = function (dataset) {
var colName = dataset.columnName;
var type = dataset.type;
console.log(colName + "::::" + type);
};
});
})();
You have to inject this module into app also.

ng-messages dont reset when resetting the form

I have a big problem in my form when I try to reset it everything is reset except the ng messages. I tried everything but nothing worked. I don't know why
Here is my form:
<md-content layout-padding>
<form name="projectForm">
<div layout="row">
<md-input-container flex="50">
<label>Name</label>
<input required name="clientName" ng-model="project.clientName" ng-focus="">
</md-input-container>
<md-input-container flex="50">
<label>Email</label>
<input required type="email" name="clientEmail" ng-model="newEmployee.email"
minlength="10" maxlength="100" ng-pattern="/^.+#.+\..+$/" />
<div ng-messages="projectForm.clientEmail.$error" role="alert" ng-show="message">
<div ng-message-exp="['required', 'minlength', 'maxlength', 'pattern']">
Your email must be between 10 and 100 characters long and look like an e-mail address.
</div>
</div>
</md-input-container>
</div>
<div layout="row">
<md-input-container flex="50">
<label>Mobile</label>
<input required name="clientName" ng-model="newEmployee.mobile">
<div ng-messages="projectForm.clientName.$error" ng-show="message">
<div ng-message="required">This is required.</div>
</div>
</md-input-container>
<md-input-container flex="50">
<label>Address</label>
<input required name="clientName" ng-model="newEmployee.address">
<div ng-messages="projectForm.clientName.$error" ng-show="message">
<div ng-message="required">This is required.</div>
</div>
</md-input-container>
</div>
<md-input-container class="md-block">
<label>Description</label>
<input md-maxlength="30" required md-no-asterisk name="description" ng-model="newEmployee.description">
<div ng-messages="projectForm.description.$error">
<div ng-message="required">This is required.</div>
<div ng-message="md-maxlength">The description must be less than 30 characters long.</div>
</div>
</md-input-container>
<div>
<md-button type="submit" class="md-raised md-primary">Submit</md-button>
<md-button ng-click="cancel()">Cancel</md-button>
</div>
</form>
</md-content>
and here is my reset function in the controller
$scope.reset = function() {
$scope.projectForm.$setPristine();
$scope.projectForm.$setValidity();
$scope.projectForm.$setUntouched();
}

Default asterisk color change inside md-input-container

I am working on md-input-container and I am trying change default asterisk color to red.
Plz take a look here
pen link
<div ng-controller="AppCtrl" layout="column" ng-cloak="" class="inputdemoErrors" ng-app="MyApp">
<md-input-container class="md-block">
<label>Description</label>
<input md-maxlength="30" required="" md-no-asterisk="" name="description" ng-model="project.description">
<div ng-messages="projectForm.description.$error">
<div ng-message="required">This is required.</div>
<div ng-message="md-maxlength">The description must be less than 30 characters long.</div>
</div>
</md-input-container>
<div layout="row">
<md-input-container flex="50">
<label>Client Name</label>
<input required="" name="clientName" ng-model="project.clientName">
<div ng-messages="projectForm.clientName.$error">
<div ng-message="required">This is required.</div>
</div>
</md-input-container>
<md-input-container flex="50">
<label>Project Type</label>
<md-select name="type" ng-model="project.type" required="">
<md-option value="app">Application</md-option>
<md-option value="web">Website</md-option>
</md-select>
</md-input-container>
</div>
<md-input-container class="md-block">
<label>Client Email</label>
<input required="" type="email" name="clientEmail" ng-model="project.clientEmail" minlength="10" maxlength="100" ng-pattern="/^.+#.+\..+$/">
<div ng-messages="projectForm.clientEmail.$error" role="alert">
<div ng-message-exp="['required', 'minlength', 'maxlength', 'pattern']">
Your email must be between 10 and 100 characters long and look like an e-mail address.
</div>
</div>
</md-input-container>
<md-input-container class="md-block">
<label>Hourly Rate (USD)</label>
<input required="" type="number" step="any" name="rate" ng-model="project.rate" min="800" max="4999" ng-pattern="/^1234$/">
<div ng-messages="projectForm.rate.$error" multiple="" md-auto-hide="false">
<div ng-message="required">
You've got to charge something! You can't just <b>give away</b> a Missile Defense
System.
</div>
<div ng-message="min">
You should charge at least $800 an hour. This job is a big deal... if you mess up,
everyone dies!
</div>
<div ng-message="pattern">
You should charge exactly $1,234.
</div>
<div ng-message="max">
{{projectForm.rate.$viewValue | currency:"$":0}} an hour? That's a little ridiculous. I
doubt even Bill Clinton could afford that.
</div>
</div>
</md-input-container>
<div>
<md-button type="submit">Submit</md-button>
</div>
<p style="font-size:.8em; width: 100%; text-align: center;">
Make sure to include ngMessages module when using ng-message markup.
</p>
</form>
The asterisk's default color is gray right next to"Client Name" label.(if you click the field, its color changes to red.) Is there any way that I can change the default color of the asterisk from gray to red?
thanks in advance,
Add the following to your CSS :
md-input-container.md-default-theme:not(.md-input-focused):not(.md-input-invalid) label.md-required:after, md-input-container:not(.md-input-focused):not(.md-input-invalid) label.md-required:after {
color:red;
}
For some reason just giving the following does not work :
label.md-required:after {
color:red;
}

form.$valid always returning false angularjs

I'm trying to make validations to my form in an ng-repeat which now always returning as false even though all the inputs are filled and correct.
Here's my update function:
$scope.updateChannel = function (channel) {
if (channelForm.$valid) {
Channels.update({
_id: channel._id
}, {
$set: {
name: channel.name,
category: channel.category,
tags: channel.tags,
logo: channel.logo
}
}, function (error, data) {
if (error) {
console.log('unable to update the channel: ' + error);
} else {
console.log('Done! ' + data);
}
});
} else {
console.log('fix errors');
}
}
and here's my form:
<form ng-show="channel.edit == true" name="channelForm" ng-submit="updateChannel(channel)" novalidate>
<md-input-container>
<label for="name">Nombre</label>
<input type="text" name="name" ng-model="channel.name" md-maxlength="20" required>
<div ng-messages="channelForm.name.$error;">
<div ng-message="required">Esto es requerido.</div>
<div ng-message="md-maxlength">Demaciados carácteres.</div>
</div>
</md-input-container>
<md-input-container class="md-block">
<label for="category">Categorias</label>
<md-select name="category" ng-model="channel.category" multiple required>
<md-option ng-repeat="kit in kits" value="{{kit.name}}">{{kit.name}}</md-option>
</md-select>
<div class="errors" ng-messages="channelForm.category.$error">
<div ng-message="required">Esto es requerido.</div>
</div>
</md-input-container>
<md-input-container>
<label for="number">Canal</label>
<input type="number" name="number" ng-model="channel.number" ng-model-options="{ updateOn: 'blur' }" maxlength="5" required>
<div ng-messages="channelForm.number.$error;">
<div ng-message="required">Esto es requerido.</div>
<div ng-message="md-maxlength">Demaciados carácteres.</div>
</div>
</md-input-container>
<md-input-container>
<label for="tags">Tags(separar con espacio)</label>
<input type="text" name="tags" ng-model="channel.tags" md-maxlength="20" required>
<div ng-messages="channelForm.tags.$error;">
<div ng-message="required">Esto es requerido.</div>
<div ng-message="md-maxlength">Demaciados carácteres.</div>
</div>
</md-input-container>
<md-input-container>
<label for="logo">Logo</label>
<input type="text" name="logo" ng-model="channel.logo" required>
<div ng-messages="channelForm.logo.$error;">
<div ng-message="required">Esto es requerido.</div>
</div>
</md-input-container>
<md-button type="submit" class="md-raised md-primary" ng-click="channel.edit = false" ng-disabled="channelForm.$invalid">Guardar</md-button>
<md-button class="md-raised" ng-click="channel.edit = false">Cancelar</md-button>
<md-button class="md-raised md-warn" ng-click="removeChannel(channel)">Borrar</md-button>
</form>
now this is in an ng-repeat="channel in channels" so I'm not sure if that's the problem.
Also, all the ng-messages seem to be working correctly and even when none of them are showing I'm still unable to update the document.
What am I doing wrong here?
Thanks!
If this is in a ng-repeat, you will end up with a bunch of forms named channelForm
You could put a new controller on the form tag and move updateChannel in that controller.

how to get the current view contents only in angularjs controller?

I'm dealing with md-tabs-wrapper it gives the partial view of each category and a single save button as follow
The existing code for it
<md-dialog aria-label="Profile edit" class="profile-edit full-height">
<form>
<md-toolbar>
<div class="md-toolbar-tools">
<h2>Edit Profile</h2>
<span flex></span>
<md-button class="md-icon-button" ng-click="hide()">
<md-icon md-font-icon="icon-close" aria-label="Close dialog"></md-icon>
</md-button>
</div>
</md-toolbar>
<md-dialog-content >
<md-tabs class=" margin-bottom-40" md-dynamic-height md-border-bottom>
<md-tab label="Profile">
<div layout layout-sm="column">
<md-input-container flex="30">
<label>First Name</label>
<input type="text" ng-model="user.firstName">
</md-input-container>
<md-input-container flex>
<label>Last Name</label>
<input type="text" ng-model="user.lastName">
</md-input-container>
</div>
<div layout layout-sm="column">
<md-input-container flex="30">
<label>Other Name</label>
<input type="text" ng-model="user.otherName">
</md-input-container>
<md-select flex class="md-select-form" ng-model="user.gender" placeholder="Gender" ch-boolean>
<md-option ng-value="gender" ng-repeat="gender in ['Male', 'Female']">{{gender}}</md-option>
</md-select>
<md-input-container flex>
<label>Date of Birth</label>
<input type="date" ng-model="user.dateOfBirth">
</md-input-container>
</div>
<md-input-container>
<label>Profile Picture</label>
<input type="file" name="file" file-model = "user.myFile">
</md-input-container>
<md-input-container flex>
<label>Summary(max 200 characters)</label>
<textarea ng-model="user.description" md-maxlength="200"></textarea>
</md-input-container>
</md-tab>
<md-tab label="Experience" ng-init="">
<h2>Employment</h2>
<div ng-repeat="experience in user.experiences">
<md-input-container>
<label>Job title</label>
<input type="text" ng-model="experience.job_title">
</md-input-container>
<div layout layout-sm="column">
<md-input-container flex="66">
<label>Employeer Name </label>
<input type="text" ng-model="experience.company_name">
</md-input-container>
<md-input-container flex="33">
<label>Location</label>
<input type="text" ng-model="experience.location_name">
</md-input-container>
</div>
<div layout layout-sm="column">
<md-input-container flex="40">
<label>Start Date</label>
<input type="date" ng-model="experience.date_start" date-ob>
</md-input-container>
<md-input-container flex="40" ng-if="experience.current_work==0">
<label>End Date</label>
<input type="date" ng-model="experience.date_end" date-ob>
</md-input-container>
<md-checkbox flex ng-model="experience.current_work" ng-true-value='1' ng-false-value='0' aria-label="Current" style="margin: 5px;">
Current
</md-checkbox>
</div>
<md-input-container flex>
<label>Job Description </label>
<textarea ng-model="experience.job_des" ></textarea>
</md-input-container>
</div>
<md-button class="md-primary md-raised" ng-click="removeTab( tab )">Add Position</md-button>
</md-tab>
<h2 class="md-subhead">Projects</h2>
<div>
<md-input-container>
<label>Project Title</label>
<input type="text" ng-model="user.jobtitle">
</md-input-container>
<div layout layout-sm="column">
<md-input-container flex="66">
<label>Employeer Name </label>
<input type="text" ng-model="user.employeer">
</md-input-container>
<md-input-container flex>
<label>Start Date</label>
<input type="date" ng-model="user.startDate">
</md-input-container>
<md-input-container flex>
<label>End Date</label>
<input type="date" ng-model="user.endDate">
</md-input-container>
</div>
<md-input-container flex>
<label>Project Details </label>
<textarea ng-model="user.jobDescription" ></textarea>
</md-input-container>
</div>
<md-button class="md-primary md-raised" ng-click="removeTab( tab )">Add Projects</md-button>
</md-tab>
<div layout layout-sm="column">
<md-input-container flex>
<label>Primary Number</label>
<input type="text" ng-model="user.primaryNumber">
</md-input-container>
<md-input-container flex>
<label>Secondary Number</label>
<input type="text" ng-model="user.secondaryNumber">
</md-input-container>
</div>
<div layout layout-sm="column">
<md-input-container flex>
<label>Primary Email</label>
<input type="text" ng-model="user.primaryNumber">
</md-input-container>
<md-input-container flex>
<label>Secondary Email</label>
<input type="text" ng-model="user.secondaryNumber">
</md-input-container>
</div>
<h2 class="md-subhead">Social Media Links</h2>
<md-input-container md-no-float>
<md-icon md-font-icon="icon-facebook-squre" style="display:inline-block;"></md-icon>
<input ng-model="user.address" type="text" placeholder="Url" >
</md-input-container>
<md-input-container md-no-float>
<md-icon md-font-icon="icon-location-on" style="display:inline-block;"></md-icon>
<input ng-model="user.address" type="text" placeholder="Url" >
</md-input-container>
<md-input-container md-no-float>
<md-icon md-font-icon="icon-location-on" style="display:inline-block;"></md-icon>
<input ng-model="user.address" type="text" placeholder="Url" >
</md-input-container>
<md-input-container md-no-float>
<md-icon md-font-icon="icon-location-on" style="display:inline-block;"></md-icon>
<input ng-model="user.address" type="text" placeholder="Url" >
</md-input-container>
</md-tab>
</md-tabs>
</md-dialog-content>
<div class="md-actions" layout="row">
<span flex></span>
<md-button ng-click="hide()" class="md-primary">
Cancel
</md-button>
<md-button ng-click="answer(this)" class="md-primary">
Save
</md-button>
</div>
</form>
</div>
</md-dialog>
In my controller i have coded to save the each tabs content as it is
$scope.answer = function(answer) {
console.log(answer);
Profile.editProfilePro($scope.user);
Profile.editProfileExp($scope.user.experiences[0]);
$mdDialog.hide(answer);
};
I want to execute the Profile.editProfileExp($scope.user) only if current view in experience
The problem is how to save the current view tabs contents only in controller answer()?? Tell me your suggestions. Thanks .
you can have a scope variable as tabsdata for active tab, so that you can identify the selected Tab and add attribute "active="tabsdata.ExperienceTab" to you html "md-tab" tag it would provide you the active Tab status, and based on your selected tab execute your code.

Resources