1

I have a template with an ng-if that checks if something is an array and an inner ng-repeat that loops over it and re-applies the same template. Otherwise if its a string it just prints the value.

Unfortunately, it seems ng-if (600) has a lower priority than ng-repeat (1000), so even with strings, the ng-repeat just calls the template over and over again (I guess with each letter of the string) and hangs the browser.

Any ideas on how to make the ng-if run in higher priority? Or any other nice way to accomplish this?

Directive

app.directive('cell', function ()
{
    return {
        restrict: "AE",
        scope: {
            cellData: '=cellData'
        },
        link: function (scope, element, attrs)
        {
            scope.isString = function (str)
            {
                return angular.isString(str);
            };
            scope.isArr = function (arr)
            {
                return !angular.isString(arr) && angular.isArray(arr);
            };
        },
        transclude: true,
        templateUrl: function (elem, attrs)
        {
            return "/template/cellData";
        }
    }

Template

span(ng-if='isArr(cellData)')
    | [
    span(ng-repeat='inner in cellData')
        cell(cellData='inner')
        span(ng-if='!$last') ,&nbsp
    | ]
span(ng-if='!isArr(cellData)')
    span(ng-bind="isString(cellData) ? '\"'+cellData+'\"' : cellData")

EDIT: I solved the problem using the code from this question. I don't really get it, but I'm happy it works.

Community
  • 1
  • 1
Madd0g
  • 3,841
  • 5
  • 37
  • 59
  • given that your ng-if & ng-repeat are on differnt elements/spans its not the priority which is the issue. issue is that ng-if is failing.. – harishr Nov 01 '14 at 05:43
  • do console.log in your isArr function.. and check whats the input, its mostly arrayOfString, the issue would be with the JSON you are receiving, i guess... – harishr Nov 01 '14 at 05:45
  • what do you mean failing? I tried console.log in isArr and the browser hangs before anything is printed, that's why I assumed it's a priority issue – Madd0g Nov 01 '14 at 05:46
  • Oh yeah, so I removed the repeat and then did a console.log and it evaluates to false correctly, so why does the browser hang? – Madd0g Nov 01 '14 at 05:49
  • cant say why browser hangs :(, so you understood the real problem, right? – harishr Nov 01 '14 at 05:50
  • haha... I think the real problem is that the browser hangs :) I don't really understand what you meant by "ng-if is failing" – Madd0g Nov 01 '14 at 05:52
  • what is that you are getting as false... can you setup a plunkr – harishr Nov 01 '14 at 05:54
  • ok. so I did some tests and it's not the `ng-repeat` that's causing the browser to hang, it's the recursion of the template inside it. If I remove that there's no hang. – Madd0g Nov 01 '14 at 06:03

0 Answers0