default settings for ng-repeat?

28 views
Skip to first unread message

Eric Eslinger

unread,
Feb 26, 2015, 11:32:24 AM2/26/15
to ang...@googlegroups.com
I use a lot of ng-repeats in my code. What I'd love to do is define somewhere at app initialization that, unless I specify otherwise: 'track by item.$id' is what I want to do, and that there should always be a filter:{_error:undefined} on the ng-repeat microsyntax as well.

I *could* probably just define my own local-repeat with transclusion that constructs the ng-repeat's transclusion. I may end up doing it that way instead, just because it's an enormous PITA to go through my code and make sure all the track by stuff is correct.

I haven't seen any documentation that would indicate this is possible on the ng-repeat directly, but I figured it's a common enough issue- drying up the microsyntax - and I might as well ask how other people are handling it.

e

Sander Elias

unread,
Feb 26, 2015, 12:01:03 PM2/26/15
to ang...@googlegroups.com

Hi Eric,

The simplest way is to write your own ngRepeat at an higher priority, and just amend it.

something like this:

    function ngRepeat($compile, $parse) {
        var ddo = {
            restrict: 'A',
            priority: 1010,
            terminal: true, //stop Compilation
            compile: compile
        };
        return ddo;

        function compile(el, attr) {
            attr.$set('ngRepeat', attr.ngRepeat + ' track by $index');
            var ngModelLink = $compile(el, null, 1010); //restart compilation at priority 9999 
            return function(scope) {
                ngModelLink(scope); // use the link function from the compilation
            };
        }
    }

See it in action in this plunk
Does that help a bit?

Regards
Sander

Eric Eslinger

unread,
Feb 26, 2015, 12:05:11 PM2/26/15
to ang...@googlegroups.com, sande...@gmail.com
Hey sander, 

That is pretty much perfect, thanks! This way I can do some string voodoo on the incoming ngRepeat attr to make sure there's the right setup, and I don't have to go and edit every single html template already in my code.

e


--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/I9bz6MmeX1U/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages