0

My problem is demonstrated in the below plnkr

Plnkr

  <ntimes repeat=10 >
      <h1>Hello World - 10 {{smsg}}</h1>
      <h4>More text</h4>
    </ntimes>

Directive :

directive('ntimes', function() {
return {
    restrict: 'E',
    scope:{
      smsg: '='
    },
    compile: function(tElement, attrs) {
        var content = tElement.children();
        for (var i = 0; i < attrs.repeat - 1; i++) {
            tElement.append(content.clone());
        }
        tElement.replaceWith(tElement.children());

        return function(scope,elem,attr){
          scope.smsg='abc';
        }
    }
}
})

I have a compile function returning link function, and the directive scope is isolated. When I update scope variable in the link function, the page is not rendering the updated value. I am expecting to see abc instead of xyz.

Please help

1 Answer 1

0

When using two-way-data-binding you have to make sure the variables are connected properly.

This means that in your directive you declare that the smsg scope variable will be two-way-data-binded with something else which will be declared in the DOM.

Now looking at the DOM this "link" is missing which provides you this error.To fix it you have to change your html to this <ntimes smsg="smsg" repeat=10 >

And when you do your new smsg will be equal to 'abc'.

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

Comments

Your Answer

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