0

I am trying to create a directive , on click of button i need to add text box but when i add 2,3 textbox they all share same scope.

How can i isolate the scope inside directive ??

http://jsfiddle.net/A8Vgk/584/

Code::

    angular.module('myApp', []).directive( 'test', function ( $compile ) {
  return {
    restrict: 'E',
    scope: { text: '@' },
    template: '<p ng-click="add()">Click me </p>',
    controller: function ( $scope, $element ) {
      $scope.add = function () {
        var el = $compile( "<input type='text' ng-model='user.name' value='hello-World!'>" )( $scope );
        $element.parent().append( el );
      };
    }
  };
});

1 Answer 1

1

Try $scope.$new() and bind your textbox to this newly created scope:

var el = $compile( "<input type='text' ng-model='user.name' value='hello-World!'>" )( $scope.$new() );

DEMO

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.