Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
22
rated 0 times [  28] [ 6]  / answers: 1 / hits: 17541  / 10 Years ago, thu, june 5, 2014, 12:00:00

I want to implement the following functionality:



Suppose you have 1 input field and 2 drop-down lists. At the input field you can fill in your e-mail address and next to that you can choose what kind of type this e-mail is (Personal, Professional, Other, or nothing).



Now in the third drop-down list you will see a list of e-mails where you can choose from, the e-mail address you prefer.



So what should happen:



1) If there is nothing in the input field the preferred e-mail drop-down list is empty (this is already the case).



2) When there is an e-mail filled in AND a TYPE, the preferred e-mail drop-down list should contain this value: [email protected] (Personal) for example.



3) When there is an e-mail filled in but no TYPE, the preferred e-mail drop-down list should contain this value: [email protected] for example so without the type.





HTML:



<div ng-repeat=email in contactInfo.emails>
<input id=email type=text ng-model=email.email/>
<select id=emailType ng-model=email.emailTypeId ng-options=emailType.id as emailType.name for emailType in emailTypes>
<option value=>Choose...</option>
</select>
</div>

<br/><br/>

<label>Preferred e-mail:</label>
<select style=margin-left: 20px; width: 50%; id=preferred-email ng-model=contactInfo.preferredEmail ng-options=email.email for email in (contactInfo.emails | filter:filterEmail) track by email.id>
<option value=>Choose...</option>
</select>




JAVASCRIPT:



function MyCtrl($scope){
$scope.contactInfo = {};
$scope.emailTypes = [{label:Personal,id:1,name:Personal,rank:2},{label:Professional,id:2,name:Professional,rank:2},{label:Other,id:3,name:Other,rank:4}];

$scope.contactInfo.emails = [{id:1100, emailTypeId:2,email:[email protected]}, {id:1200, emailTypeId:1,email:[email protected]}];
$scope.contactInfo.prefferedEmail = {};

$scope.filterEmail = function(email){
return (email.email);
}
}




JSFIDDLE:



HERE is the fiddle but only the first one is working.



I don't have ant clue so it would be great if someone could help me with this. Thank you for your time.



Sven.


More From » angularjs

 Answers
32

Here is a sample implementation - http://jsfiddle.net/iamgururaj/T7fkH/5/



Code:



<select style=margin-left: 20px; width: 50%; id=preferred-email ng-model=contactInfo.preferredEmail ng-options=getEmail(email) for email in (contactInfo.emails | filter:filterEmail) track by email.id>
<option value=>Choose...</option>
</select>


JS:



$scope.contactInfo = {
emails: [{
id: 1100,
emailTypeId: 2,
email: [email protected]
}, {
id: 1200,
emailTypeId: 1,
email: [email protected]
}]
};
$scope.emailTypes = {
1: Personal,
2: Professional,
3: Other
};
$scope.filterEmail = function (email) {
return (email.email);
}

$scope.getEmail = function (email) {
if (email.emailTypeId) {
return email.email + ' (' + $scope.emailTypes[email.emailTypeId] + ')';
}
return email.email;
}

[#70707] Wednesday, June 4, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
quentinaveryb

Total Points: 102
Total Questions: 100
Total Answers: 93

Location: Colombia
Member since Mon, May 2, 2022
2 Years ago
quentinaveryb questions
Thu, Aug 6, 20, 00:00, 4 Years ago
Fri, Jul 17, 20, 00:00, 4 Years ago
Mon, Aug 12, 19, 00:00, 5 Years ago
;