1

Inside angularJS directive I'm trying to iterate over array and based on values I would like to create nested list of directives. Current version of directive

Directive type

    .directive("type", function($compile, $log){
        return{
            restrict: "E",
            replace: true,
            transclude: true,
            scope: {
                type: '='
            },
            template: "<div></div>",
            link: function(scope, element, attrs){
                if (angular.isArray(scope.type)){
                    angular.forEach(scope.type, function(value, index){
                        $log.error(value);
                        element.append("<type type='scope.type['"+index+"]'></type>");
                    });
                } else if (angular.isObject(scope.type)){
                    element.append("OBJECT")
                } else {
                    element.append("<div>{{scope.type}}</div>")
                }
                $compile(element.contents())(scope)
             }
        };
      })

I also tried to use above directive with next version of link function:

if (angular.isArray(scope.type)) {
    element.append("<div ng-repeat='element in scope.type'><type type='element'></type></div>");
} else if (angular.isObject(scope.type)) {
    element.append("OBJECT")
} else {
    element.append("<div>{{scope.type}}</div>")
}
$compile(element.contents())(scope)
}

None of provided codes solve my issue. Below you will find example explaining on specific case:

Let's say that I had next object in the scope.type = [null,"int"]. Now I would like to use <type type='type'><type> and as a result of first evaluation I want to have sth like: <type type='type[0]'></type><type type='type[1]'></type> Further evaluation of those values should lead to some simpler form but right now it is not important.

How I can achieve sth like this?

Edit

I tried even to exctract part of the code responsible for iteration to the seperate directive but it still does not work. Code:

Update link function in type directive:

link: function(scope, element, attrs) {
    if (angular.isArray(scope.type)) {
        element.append("<typeb type='scope.type'></typeb>")
    } else if (angular.isObject(scope.type)) {
        element.append("OBJECT")
    } else {
        element.append("<div>{{scope.type}}</div>")
    }
    $compile(element.contents())(scope)
}

New directive:

.directive("typeb", function($compile, $log){
      return{
          restrict: "E",
          replace: true,
          transclude: true,
          scope: {
              type: '='
          },
          template: "<div ng-repeat='t in type'>{{t}}</div>",
      };
    })

Problem still occurs but generated html contains only next pieces as a result of typeb directive: <!-- ngRepeat: t in type -->

2
  • try element.append($compile("<div ng-repeat='element in scope.type'><type type='element'></type></div>")(scope)); Commented Jan 28, 2015 at 9:38
  • It does not help me. Interesting thing is that in html I see something like that: <!-- ngRepeat: el in scope.type --> in case that scope.type is equal to ["double","null"] Commented Jan 28, 2015 at 9:48

2 Answers 2

1

The problem you are getting is <!-- ngRepeat: t in type --> this is because your type didn't contains any value, when it is inside typeb directive

Your directive shouldn't be use scope.variable on view. Scope variable will be directly accessible by their name like

{{type}} or <typeb type='type'></typeb>

Change your link code to below.

Directive link

link: function(scope, element, attrs) {
    if (angular.isArray(scope.type)) {
        element.append("<typeb type='type'></typeb>")
    } else if (angular.isObject(scope.type)) {
        element.append("OBJECT")
    } else {
        element.append("<div>{{type}}</div>")
    }
    $compile(element.contents())(scope)
}

Thanks.

Sign up to request clarification or add additional context in comments.

Comments

0

Use an ng-repeat in the template

<your-directive attr="item.someattr" ng-repeat="item in items"></your-directive>

.directive("type", function($compile, $log){
    return{
        restrict: "E",
        replace: true,
        transclude: true,
        scope: {
            type: '='
        },
        template: "NG REPEAT HERE",
        ...
  })

4 Comments

Where exactly? I am using ng-repeat in link function (second example) but it does not work.
Updated it. Basically, in the outer scope of where you want your repeated directives. You have this outer directive type, use it's template to ng-repeat the sub directives
The thing is that value of the attribute passed to the type directive it is not always array or object - sometimes it could be just single string like: 'int'. It will tackle such situation?
Allow your sub directive to deal with it's actual type, displaying itself in the correct way. Separate the repeating behaviour from the type specific display behaviour

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.