Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ng-repeat inside template does not seem to work (demo) #43

Open
pandaiolo opened this issue Apr 4, 2014 · 4 comments
Open

ng-repeat inside template does not seem to work (demo) #43

pandaiolo opened this issue Apr 4, 2014 · 4 comments

Comments

@pandaiolo
Copy link

Why is it not possible to use ng-repeat inside a deckgrid while ng-repeat can be used recursively ?

Demonstration of both non-working (deckgrid) and working (ng-repeat) behaviour here : (http://codepen.io/anon/pen/nfzHk/)

@pandaiolo
Copy link
Author

Seem related to this anguar issue. I tried the mentioned workaround which is to wrap ng-repeat element if it is the card's root element, but with no success...

@pandaiolo
Copy link
Author

As a quick workaround, for anyone interested, here is a very basic directive to replace ng-repeat and that does not creates a new scope while iterating through card items

    // Example markup : <div no-scope-repeat items="card.items"></div>
    .directive('noScopeRepeat', function($compile) {
        return function(scope, elem, attrs) {
            scope.$watch(attrs.items, function(items) {
                if (!items) return;

                // Example template that will be repeated, with above markup,  
                // #OBJ#.myValue will be replaced by card.items[idx].myValue at each step
                var template = '<div>{{ #OBJ#.myValue }}</div>';

                items.forEach(function(val, key) {
                    var newElement = angular.element(
                        template.replace(/#OBJ#/g, attrs.items + '[' + key + ']')
                    );
                    $compile(newElement)(scope);
                    elem.append(newElement);
                });
            });
        };
    })

Anyone makes a transcluded version I'm interested :-)

@tplaindoux
Copy link

@pandaiolo This is how I implemented it. I'm not sure this is the most efficient way but it allows me to use custom template import, more flexible.

template
note that this is included in the card template.

<ANY no-scope-repeat items='card.itemList'></ANY >

included template (partial_comment_list.html)

<div class="feed-box" ng-repeat="comment in items">
 ...your code like any ng-repeat...
 </div>

directive

  'use strict';

angular.module('siteClientApp')
  .directive('noScopeRepeat', function () {
    return {
      restrict: 'AE',
      templateUrl: 'partial/normal_partial.html',
      scope: { items: '=items'},
      compile: function CompilingFunction($templateElement) {
        $templateElement.replaceWith(this.template);
        return function postLink() {
          return true;
        };
      }
    };
  });

UPDATE: directive is now working as it should.
You just have to .push() data to the card.itemList to update the template.

example:

 $scope.feeds[itemId].comments.push(data.comment);

@zenithtekla
Copy link

Indeed, it does work. I was able to have ng-repeat div work within the template file and render different templates for different listing styles. I have experience and confidence working with Directives in AngularJS.
You are welcome to check out my repo for solution to your end: my repo - R37

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants