{"id":15,"date":"2016-06-24T18:10:37","date_gmt":"2016-06-24T22:10:37","guid":{"rendered":"http:\/\/langstonsoftware.com\/?p=15"},"modified":"2017-06-03T09:28:21","modified_gmt":"2017-06-03T13:28:21","slug":"angularjs-readonly-dropdown","status":"publish","type":"post","link":"https:\/\/langstonsoftware.com\/2016\/06\/24\/angularjs-readonly-dropdown\/","title":{"rendered":"AngularJS Readonly Dropdown"},"content":{"rendered":"

HTML select’s don’t implement the readonly attribute, so the only way to prevent them from being edited is to use the disabled attribute. Doing so however prevents them from being posted on submission. The standard solution for this is create a hidden input field, which you have to keep in sync with the disabled select element. Finally, if your read-only status is dynamically changing based on the editable values in other fields, you’ll have to disable\/re-enable the proper fields on the client. AngularJS can be used to get some of this logic contained.<\/p>\n

Here’s an example:<\/p>\n

HTML:<\/p>\n

\r\n<div ng-app="app">\r\n\t<div ng-controller="dropdownController">\r\n\t\t<select name="cat" ng-model="cat" ng-options="item.value as item.label for item in cats" ng-disabled="catDisabled">  \r\n    <\/select>\r\n\t\t<input name="cat" type="hidden" ng-disabled="!catDisabled" ng-model="cat">\r\n\t\t<input type="text" ng-model="cat"\/>\r\n    <br\/>\r\n    <label><input type="checkbox" ng-model="catDisabled"> Disable?<\/label>\r\n\t<\/div>\r\n<\/div>\r\n<\/pre>\n

JavaScript:<\/p>\n

\r\nangular.module('app', []).controller('dropdownController', function($scope){\r\n  $scope.form = [];\r\n  \r\n  $scope.cats = [\r\n    {value: 1, label: 'Mufasa'},\r\n    {value: 2, label: 'Simba'},\r\n    {value: 3, label: 'Scar'},\r\n    {value: 4, label: 'Fluffy'}\r\n  ];\r\n});\r\n<\/pre>\n

JsFiddle:
\nhttps:\/\/jsfiddle.net\/thomaslangston\/wgaf9v3q\/<\/a><\/p>\n

Before you go<\/h3>\n

 
\nI’d like to get your feedback! Please take a moment to post a comment below to let me know your thoughts on this post. Anything you think should be introduced to a beginner or junior developer ASAP? Or is there a facet to the business of software I’ve overlooked? I would be very happy to hear about either.<\/p>\n

Also, I putting together a very focused mailing list on the topics that I regularly cover on this blog. If this post was useful to you, please sign up here<\/em> <\/a>to get future emails from me on this and related subjects. Thanks!<\/p>\n","protected":false},"excerpt":{"rendered":"

HTML select’s don’t implement the readonly attribute, so the only way to prevent them from being edited is to use the disabled attribute. Doing so however prevents them from being posted on submission. The standard solution for this is create a hidden input field, which you have to keep in sync with the disabled select …<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[6,7],"tags":[],"class_list":["post-15","post","type-post","status-publish","format-standard","hentry","category-angularjs","category-javascript"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p6Os38-f","_links":{"self":[{"href":"https:\/\/langstonsoftware.com\/wp-json\/wp\/v2\/posts\/15"}],"collection":[{"href":"https:\/\/langstonsoftware.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/langstonsoftware.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/langstonsoftware.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/langstonsoftware.com\/wp-json\/wp\/v2\/comments?post=15"}],"version-history":[{"count":14,"href":"https:\/\/langstonsoftware.com\/wp-json\/wp\/v2\/posts\/15\/revisions"}],"predecessor-version":[{"id":217,"href":"https:\/\/langstonsoftware.com\/wp-json\/wp\/v2\/posts\/15\/revisions\/217"}],"wp:attachment":[{"href":"https:\/\/langstonsoftware.com\/wp-json\/wp\/v2\/media?parent=15"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/langstonsoftware.com\/wp-json\/wp\/v2\/categories?post=15"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/langstonsoftware.com\/wp-json\/wp\/v2\/tags?post=15"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}