Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Update:</h2> <blockquote> <p>Try to click on elem3 : You will see the console.debug output 'Object {name: "elem3", children: Array[0]' And the console.warn outputs : Object {name: "elem2", children: Array[1] I though I would had "elem3" output twice. Instead I got "elem3" and "elem2". Do you know why this happens?</p> </blockquote> <p>To anwser this, first we take a look on generated HTML and scope:</p> <pre><code> &lt;!-- controller scope --&gt; &lt;li data-ng-repeat=" tr in trData "&gt; &lt;!--1|-child scope --&gt; &lt;span&gt;elem2 - &lt;/span&gt; &lt;!--2 |-isolated scope --&gt; &lt;ul&gt; &lt;!-- | --&gt; &lt;li data-ng-repeat="tr0 in tr.children"&gt;&lt;!--3 |-child scope --&gt; &lt;span&gt;elem3&lt;/span&gt; &lt;!--4 |-isolated scope --&gt; &lt;/li&gt; &lt;/ul&gt; &lt;/li&gt; </code></pre> <p>The variables in scope will be like: (<code>|-</code> stands for inheritance)</p> <ul> <li>Controller Scope: <code>{ trData: [...] }</code> <ul> <li>|- Scope1 (child): <code>{ tr0: elem2 }</code></li> </ul></li> <li>Scope2 (isolated): <code>{ tr: elem2, test: function() }</code> <ul> <li>|- Scope3 (child): <code>{ tr0: elem3 }</code></li> </ul></li> <li>Scope4 (isolated): <code>{ tr: elem3, test: function() }</code> </li> </ul> <p><br> Now, according to <a href="http://docs.angularjs.org/api/ng.%24compile" rel="nofollow">official api docs</a>, which talks about isolated scope variable:</p> <blockquote> <p>&amp; or &amp;attr - provides a way to execute an expression in the context of the parent scope.</p> </blockquote> <p>* This feature is implemented by <code>$parse</code> service.</p> <p>After you clicked on <code>elem3</code>, the execution order will be: (skip the log part.)</p> <ol> <li><code>test({ tr: scope.tr })</code> using <code>Scope4</code> as its execution context <ul> <li>Equals to <code>$parse('test(tr)')(Scope3, { tr: elem3 })</code>.</li> <li>It's called in <code>Scope3</code> because <code>$parse</code> executes in parent scope.</li> </ul></li> <li><code>test(tr)</code> using <code>Scope3</code> as its execution context <ul> <li>Equals to <code>$parse('testFunction(tr)')(Scope1, elem3)</code> </li> <li>It's called in <code>Scope1</code> because test() is inherited from <code>Scope2</code>, and <code>$parse</code> executes in parent scope.</li> </ul></li> <li><code>testFunction(tr)</code> using <code>Scope1</code> as its execution context <ul> <li>Now tr will be parsed into <code>Scope1.tr</code> which is <code>elem2</code> because <code>elem3.tr</code> doesn't exist.</li> </ul></li> <li>Finally, <code>console.warn(elem2)</code>;</li> </ol> <p>This is how that happens... </p> <p>* About $parse service you can check <a href="http://docs.angularjs.org/api/ng.%24parse" rel="nofollow">this</a>.</p> <hr> <h2>Origin:</h2> <p>I changed <code>console.log</code> to <code>console.warn</code>, ang log shows it only executed once.</p> <pre><code> $scope.testFunction = function (tr) { console.warn(tr); } </code></pre> <p><a href="http://jsfiddle.net/T4uKf/" rel="nofollow">http://jsfiddle.net/T4uKf/</a></p> <p>It prints twice</p> <pre><code>A {name : 'elem3',children : []} {name : 'elem2',children : [{name : 'elem3',children : []}] } B </code></pre> <p>only because you log it in both <code>scope.setActive</code> and <code>$scope.testFunction()</code></p>
    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.
 

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