1

I was working on angularjs and i got strucked in using jquery with angularjs. Here is my html code.....

 <div class="packery ">
  <div class="item"></div>
  <div class="item h4"></div>
  <div class="item w2"></div>
  <div class="item"></div>
  <div class="item h2"></div>
  <div class="item w2 h2"></div>
  <div class="item"></div>
  <div class="item w4"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item h4"></div>
  <div class="item w2"></div>
  <div class="item"></div>
  <div class="item h2"></div>
  <div class="item w2 h2"></div>
  <div class="item"></div>
  <div class="item w4"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item h2"></div>
  <div class="item w2 h2"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item h2"></div>
  <div class="item w2"></div>
  <div class="item"></div>
</div>

here is my js code............

$( function() {


  var packery = $('.packery').packery();

  console.log(packery);

  packery.on( 'click', function( event ) {
    // remove clicked element
    packery.packery( 'remove', event.target );
  });
});

Please help me convert this jquery code to work in angularjs. I am new to jquery, I tried many ways to solve this.

Thanks in advance...

2
  • Is packery a JQuery plugin? Commented Apr 22, 2015 at 18:20
  • yes it was packery jquery plugin.. Commented Apr 23, 2015 at 5:23

4 Answers 4

1

I assume you have included jQyery

       $.noConflict();
       jQuery(function() {
           jQuery('.packery').on('click',function(){
              jQuery(this).remove();
           });
       });

Fiddle

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

3 Comments

thanks for the reply, but i m getting an error like $ is not defined. how to make use of this code in angular.. $( function() { var packery = $('.packery').packery(); console.log(packery); packery.on( 'click', function( event ) { // remove clicked element packery.packery( 'remove', event.target ); }); });
This why I said, did you included jQuery in document head. also check if $ is not used. I am editing my answer
You need to add jQuery library specifying the path of the source, something like this <script src="//code.jquery.com/jquery-1.9.1.min.js"></script>. This will download the file from jquery's cdn but you can give the local path in your project
1

For a simple click event you need ng-click within your template described at https://docs.angularjs.org/api/ng/directive/ngClick

No need for jQuery in this case

Comments

0

If you want to simply remove the clicked element, use this:

Your HTML:

<div class="packery">
   <div class="item" data-ng-click="remove($event)"></div>
   <div class="item h4" data-ng-click="remove($event)"></div>
   .......
</div>

Your Controller:

function myController( $scope ){
   $scope.remove=function($event){ 
       $event.target.remove();    
  }       
}

Check our the Fiddle example.

Comments

0

This is a good use case for an Angular directive. You can use an html attribute to specify which element the jQuery plugin should be applied to:

angular.module("mymod")
.directive("packery", function(){
  return {
    link: function(scope, element, attrs) {
      $('[packery]').packery().on('click', function(event){
        $(element).packery('remove', event.target);
      });
    }
  }
});

Then, whenever you want this jQuery plugin to be applied to an element, you simply give that element the packery attribute:

<div packery>...</div>

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.