0

Is there any way to duplicate code in Angujar JS ng-click without using a directive? This is my code I want to clone:

<div class="panel">
        <div class="row">
            <div class="small-5 columns">
                <select ng-model="vm.playlist" ng-options="lang.name for lang in vm.playlist"></select>
            </div>
            <div class="small-5 columns">
                <input type="text" placeholder="playlist" ng-model="vm.target"/>
            </div>
            <div class="small-2 columns" ng-click="rootVm.openModal()">
                <img src="assets/img/mission/browse.png" ng-model="vm.icon" />
            </div>
        </div>
</div>

On ng-click, I suppuse that I have to call a function in my controller. So, how the code is duplicated?

If finally had to do it with a directive, how it would be?

Cheers!

2
  • 2
    This is like asking how to code in Java without using methods ...why do you not want to use a directive? Are you aware that you are using about half a dozen directives in the code you posted? Commented Jun 15, 2016 at 12:25
  • Sounds like you need to take a look at ng-repeat Commented Jun 15, 2016 at 12:27

2 Answers 2

1

You can clone using the button click on ng-click by specifying the sourceId (mainDiv) and targetId (cloneDiv):

HTML:

<button ng-click="clickToClone('mainDiv', 'cloneDiv')">
    Click to Clone
</button>

JS:

$scope.clickToClone = function(sourceId, cloneId) {
    var sourceHtml = angular.element(document.getElementById(sourceId)).html();
    angular.element(document.getElementById(cloneId)).append(sourceHtml);
}

Watch the demo here.

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

Comments

0

Although you should go for ng-repeat and increment the the model of the ng-repeat on ng-click but if still you want to do on click wihtout changing the model then you can call a function like clonerow in controller

 $scope.clonerow = function()
 {
    var parentElement = angular.element( document.querySelector( '.panel' ) );
    var tobeClonedElement = angular.element( document.querySelector( '.row'   ) );
    parentElement.append(tobeClonedElement.clone());
 } 

Check the plunker

https://plnkr.co/edit/gHIwJ0llklD8BgEsM6Gs?p=preview

3 Comments

You might want to save your plunkr before posting the link - if I click on the link, I just see the angular "hello {{name}}" template ...
Are you sure this sets up proper scopes for the nested directives?
not sure where with plunker is gone since i took my code from there only to mention here as well. will be updating it again. and regarding scope , i have already told the OP the right way is to go by data driven method (ng-model) , the code was just for his knowledge and nothing else

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.