Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Those are all examples of services that aid in AngularJS view rendering (although <code>$parse</code> and <code>$interpolate</code> could be used outside of this domain). To illustrate what is the role of each service let's take example of this piece of HTML:</p> <pre><code>var imgHtml = '&lt;img ng-src="/path/{{name}}.{{extension}}"&gt;' </code></pre> <p>and values on the scope:</p> <pre><code>$scope.name = 'image'; $scope.extension = 'jpg'; </code></pre> <p>Given this markup here is what each service brings to the table:</p> <ul> <li><code>$compile</code> - it can take the whole markup and turn it into a linking function that, when executed against a certain scope will turn a piece of HTML text into dynamic, live DOM with all the directives (here: <code>ng-src</code>) reacting to model changes. One would invoke it as follows: $compile(imgHtml)($scope) and would get a DOM element with all the DOM event bounds as a result. <code>$compile</code> is making use of <code>$interpolate</code> (among other things) to do its job.</li> <li><code>$interpolate</code> knows how to process a string with embedded interpolation expressions, ex.: <code>/path/{{name}}.{{extension}}</code>. In other words it can take a string with interpolation expressions, a scope and turn it into the resulting text. One can think of the <code>$interpolation</code> service as a very simple, string-based template language. Given the above example one would use this service like: <code>$interpolate("/path/{{name}}.{{extension}}")($scope)</code> to get the <code>path/image.jpg</code> string as a result.</li> <li><code>$parse</code> is used by <code>$interpolate</code> to evaluate individual expressions (<code>name</code>, <code>extension</code>) against a scope. It can be used to both <em>read</em> and <em>set</em> values for a given expression. For example, to evaluate the <code>name</code> expression one would do: <code>$parse('name')($scope)</code> to get the "image" value. To set the value one would do: <code>$parse('name').assign($scope, 'image2')</code></li> </ul> <p>All those services are working together to provide a live UI in AngularJS. But they operate on different levels:</p> <ul> <li><code>$parse</code> is concerned with individual expressions only (<code>name</code>, <code>extension</code>). It is a read-write service.</li> <li><code>$interpolate</code> is read only and is concerned with strings containing multiple expressions (<code>/path/{{name}}.{{extension}}</code>)</li> <li><code>$compile</code> is at the heart of AngularJS machinery and can turn HTML strings (with directives and interpolation expressions) into live DOM.</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload