0

I'm tying to clone a text input as the user focuses on the last input from that container (col-md-5).

<div class="form-group">
<label class="col-md-3 control-label">Phone</label>
<div class="col-md-5">
    <input type="text" class="form-control" name="mobile[]" ng-focus="add()" />
</div>

angular.module('test')
.controller('TestController', function ($scope) {
    $scope.add = function () {
        //can't find any solution...
   }
})

I've put up a fiddle: http://jsfiddle.net/y3mdsf0t/ I can't find any way to do that...Please help

1 Answer 1

1

If you want to clone an input for example, you can do the following by creating a directive :

Directive

(function(){

  function clone($compile) {
    return {
        restrict: "A",
        link: function(scope, element, attrs) {
          //Retrieve and create angular element
          var elm = angular.element(element[0]);

          //Add event listener for a focus event
          elm.on('focus', function(){

            //Create newElm by copying our elm
            var newElm = $compile(elm.clone())(scope);

            //Emit event clone with the clone elm
            scope.$emit('clone', newElm);
          });

        }
    };
  }

angular
  .module('app')
  .directive('clone', clone);


})();

Then how send your clone data to your Controller ? By using $emit and $on.

Then with $emit you will be able to dispatch event from child controller to scopes upwards.

With $on, you will be able to listen a specific event, and retrieve the data which will be passed in the $emit.

Controller

(function(){

function Controller($scope) {


  //Listen for a clone event
  $scope.$on('clone', function(event, data){

    //Here data is your cloned input
    //print my clone input
    console.log(data);
  });

}

angular
.module('app', [])
.controller('ctrl', Controller);

})();

And apply the direcive to your HTML element :

HTML

<input type="text" class="form-control" name="mobile[]" clone />
Sign up to request clarification or add additional context in comments.

4 Comments

awsome, thanks. I really need to get deeper in directives
is there a way to enable focus only on the last input? My problem is that when I blur the first element and focus it again, it clones another one.
I'm trying to set a fiddle here: jsfiddle.net/23crr7ez/5 but something is wrong with the name of the module. In that fiddle there's an issue with cloning the first element, although I'm changing nd-model attribute, it still copy/pastes all that I write in the first one
You should put the DOM manipulation into your directive. Moreover, you can use a factory provider to handle the count of input for your new ngModel attribute. Then, you can remove the clone attribute directive to enable focus only one the last input. See the Plunker

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.