0

i tried to add a ng-repeat directive in a compile method of other directive but it is not work.

here's a demo: http://jsfiddle.net/yiooxir/mdptnqo5/1/

I expected that the 'field' directive be fruitful in the three fields (input) but this is not happening

here is my code:

html:

<div ng-app='myApp' ng-controller='testCtrl'>
   {{object.var}}
   <field value='object.var' plural></field>
</div>

js:

app.controller('testCtrl', function ($scope) {
   $scope.object = {
      var: [1, 2, 3]
   }
 });

app.directive('field', function($parse) {
  return {
    restrict: 'AE',
    scope: {
        value: '='
    },
    template: "<div><input type='text' ng-model='value'></div>",
    link: function() {}
  }
})

app.directive('plural', function($compile){
  return {
    priority: 1001,
    compile: function(element, attr) {
        element.attr({
            'ng-repeat': 'i in object.var track by $index',
            'value': 'object.var[$index]',
            'button': ''
        });
    }
  }
})

1 Answer 1

1

in plural directive at the end of compile function

    return function(scope, element) { 
      $compile(element.contents())(scope);
    }

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

1 Comment

yes. this can also be done if we add the following code as shown below: <pre> compile: function(element, attr) { element.attr({ 'ng-repeat': 'i in object.var track by $index', 'value': 'object.var[$index]', }); var parent = element.parent(); parent.append(element) </pre> but it is unclear why so ? most of other directives don't recuire it

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.