Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to create a list from parsed HTML text with Angular.js?
    primarykey
    data
    text
    <p>For my application, I need to load data from HTML. On a list element I need to parse the UL children elements (LI) and serialize those elements (map to array) in a model. From this model I will create a modal view, recreate a list and modify this list.</p> <p>I use JQuery and AngularJs. I have created a directive and parse the list when initializing and store everything in a model. But when creating a new list from the array with a template and ng-repeat, I get original data + 3 extra values.</p> <p><strong>Html:</strong></p> <pre class="lang-html prettyprint-override"><code>&lt;div ng-app="MyApp"&gt; &lt;ul ng-my-list&gt; &lt;li&gt;One&lt;/li&gt; &lt;li&gt;Two&lt;/li&gt; &lt;li&gt;Three&lt;/li&gt; &lt;li&gt;Four&lt;/li&gt; &lt;/ul&gt; &lt;/div&gt; </code></pre> <p><strong>Directive:</strong></p> <pre class="lang-js prettyprint-override"><code>app = angular.module "MyApp", [] app.directive 'ngMyList', -&gt; linker = (scope, element, attrs) -&gt; scope.items = { entry: element.find('li').map -&gt; $(this).text() } { template: '&lt;ul&gt;&lt;li ng-repeat="item in items.entry"&gt; &gt; {{item}}&lt;/li&gt;&lt;/ul&gt;' replace: true restrict: 'A' link: linker scope: {} } angular.bootstrap document, ['MyApp'] </code></pre> <p>I have extracted the code to <a href="http://jsfiddle.net/sbusso/NRLUH/" rel="nofollow">http://jsfiddle.net/sbusso/NRLUH/</a>. In this example I can't even get values from original list. </p> <p>What s wrong in this code? How can I get ng-repeat ignoring extra array values?</p> <p><strong>UPDATE</strong></p> <p>adding a splice(0) clean the array and I get my result</p> <pre><code>entry: element.find('li').map (-&gt; $(this).text()).splice(0) </code></pre> <p>Is there something specific between AngularJS and JQuery ? this code was working with Backbone.</p> <p><strong>UPDATE 2</strong></p> <p>In example I have to use context to get same result as my application:</p> <pre><code> scope.items = { entry: $(element.context).find('li').map( -&gt; $(this).text() ).splice(0) } </code></pre> <p>Only issue still: why map / ng-repeat doenst play well together (if I dont add splice(0)) ?</p> <p>updated code to show working list + 3 extra lines: <a href="http://jsfiddle.net/sbusso/NRLUH/5/" rel="nofollow">http://jsfiddle.net/sbusso/NRLUH/5/</a></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