{"id":467,"date":"2022-08-30T15:01:50","date_gmt":"2022-08-30T15:01:50","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/angularjs-kendoui-dropdownlist-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:01:50","modified_gmt":"2022-08-30T15:01:50","slug":"angularjs-kendoui-dropdownlist-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/angularjs-kendoui-dropdownlist-collection-of-common-programming-errors\/","title":{"rendered":"Angularjs + kendoui dropdownlist-Collection of common programming errors"},"content":{"rendered":"<p>I have this directive<\/p>\n<pre><code>angular.module('xxx', [\n])\n    .directive('qnDropdown', [\n        '$parse',\n        function($parse) {\n            return {\n                restrict: 'A',\n                require: 'ngModel',\n                link: function(scope, elem, attr, ngModel) {\n                    scope.$watch(attr.qnDropdown, function(source) {\n                        var model = $parse(attr.ngModel);\n\n                        elem.kendoDropDownList({\n                            dataTextField: \"Name\",\n                            dataValueField: \"ID\",\n                            value: attr.value,\n                            select: function(e) {\n                                var item = this.dataItem(e.item.index());\n                                scope.$apply(function() {\n                                    model.assign(scope, item.value);\n                                });\n                            },\n                            \/\/template: '<strong>${ data.Name }<\/strong><br \/>${ data.ID }<br \/><br \/>',\n                            dataSource: source\n                        });\n\n                    });\n                }\n            };\n        }]);\n<\/code><\/pre>\n<p>Input field is<\/p>\n<pre><code>\n<\/code><\/pre>\n<p>EVerything works fine but initial value for kendoDropDownList is not filled (value: attr.value). I suppose I am doing something at wrong place or time but not sure what?<\/p>\n<ol>\n<li>\n<p>You probably need to use <code>$observe<\/code>:<\/p>\n<blockquote>\n<p>Use <code>$observe<\/code> to observe the value changes of attributes that contain interpolation (e.g. <code>src=\"{{bar}}\"<\/code>). Not only is this very efficient but it&#8217;s also the only way to easily get the actual value because during the linking phase the interpolation hasn&#8217;t been evaluated yet and so the value is at this time set to undefined. &#8212; docs, see section <strong>Attributes<\/strong>.<\/p>\n<\/blockquote>\n<p>Here&#8217;s an example where I used $observe recently. See also @asgoth&#8217;s answer there, where he uses $watch, but he also created an isolate scope.<\/p>\n<p>I&#8217;m still not clear on when we need to use $observe vs when we can use $watch.<\/p>\n<\/li>\n<li>\n<p>Are you sure <code>{{installation.LocationID}}<\/code> has a value you expect? I was able to copy-paste your code with some tweaks for my situation and the dropdownlist is working wonderfully (thank you for doing the hard work for me!). I&#8217;m populating <code>value<\/code> on the input field and when the directive executes, <code>attr.value<\/code> has it and Kendo shows it as expected. Perhaps this was an Angular issue a couple versions ago?<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 19:44:03. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I have this directive angular.module(&#8216;xxx&#8217;, [ ]) .directive(&#8216;qnDropdown&#8217;, [ &#8216;$parse&#8217;, function($parse) { return { restrict: &#8216;A&#8217;, require: &#8216;ngModel&#8217;, link: function(scope, elem, attr, ngModel) { scope.$watch(attr.qnDropdown, function(source) { var model = $parse(attr.ngModel); elem.kendoDropDownList({ dataTextField: &#8220;Name&#8221;, dataValueField: &#8220;ID&#8221;, value: attr.value, select: function(e) { var item = this.dataItem(e.item.index()); scope.$apply(function() { model.assign(scope, item.value); }); }, \/\/template: &#8216;${ data.Name }${ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-467","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/467","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=467"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/467\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=467"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=467"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=467"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}