{"id":2184,"date":"2022-08-30T15:22:45","date_gmt":"2022-08-30T15:22:45","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/01\/04\/angularjs-evaluate-an-interpolation-expression-without-printing-to-screen-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:22:45","modified_gmt":"2022-08-30T15:22:45","slug":"angularjs-evaluate-an-interpolation-expression-without-printing-to-screen-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/angularjs-evaluate-an-interpolation-expression-without-printing-to-screen-collection-of-common-programming-errors\/","title":{"rendered":"AngularJS evaluate an interpolation expression without printing to screen-Collection of common programming errors"},"content":{"rendered":"<p>Instead of a temporary variable, call a method when you need the value, and in this method, use a cached version of the result, or call your expensive method if the cached version is null\/undefined. It will only call your expensive function once per iteration, and it won&#8217;t call it if it&#8217;s not needed. It won&#8217;t print it unless you want to. Because each iteration of a ng-repeat spawns a new scope, each iteration will start with an empty cache.<\/p>\n<p>e.g.:<\/p>\n<pre><code>$scope.cache = null\n$scope.getValue = function() {\n  if (!this.cache) { \/\/ use typeof if 0 or false or empty string is valid!\n    this.cache = myExpensiveFunc()\n  }\n  return this.cache\n}\n<\/code><\/pre>\n<p>Not 100% sure $scope.cache = null will be initialized for each scope of the iteration, you may need to check if &#8216;this&#8217; hasOwnProperty &#8216;cache&#8217; in getValue. So in your view, you only reference getValue() when you need to.<\/p>\n<p>HTH!<\/p>\n<p>edit: cache on scope, not in controller.<\/p>\n<p id=\"rop\"><small>Originally posted 2014-01-04 02:52:11. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>Instead of a temporary variable, call a method when you need the value, and in this method, use a cached version of the result, or call your expensive method if the cached version is null\/undefined. It will only call your expensive function once per iteration, and it won&#8217;t call it if it&#8217;s not needed. It [&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-2184","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2184","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=2184"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2184\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2184"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2184"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2184"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}