0

Ia m new in Angular, but i know to work in Jquery, i have problem with converting some function into Angular directive. Can someone help me about that, this is my jquery function

$('.spinner .btn:first-of-type').on('click', function() {
  $(this).closest('.spinner').find('input').val(function(i, v) {
    return parseInt(v, 10) + 1;
  });
});
$('.spinner .btn:last-of-type').on('click', function() {
  $(this).closest('.spinner').find('input').val(function(i, v) {
    return parseInt(v, 10) - 1;
  });
});

2 Answers 2

1
angular.module("yourmodulname").directive("spinnerDirective", spinnerDirective);

function spinnerDirective(){
   return { //Edit -1
 restrict:"A", //restrict the directive to the attribute
 link: "spinnerDirectiveLink"
    }//Edit-1
}

function spinnerDirectiveLink(scope, element, attribute){


 $('.spinner .btn:first-of-type').on('click', function() {
      $(this).closest('.spinner').find('input').val(function(i, v) {
        return parseInt(v, 10) + 1;
      });
    });
    $('.spinner .btn:last-of-type').on('click', function() {
      $(this).closest('.spinner').find('input').val(function(i, v) {
        return parseInt(v, 10) - 1;
      });
    });

}

HTML

<p spinner-directive></p>

Since your question is not clear on the task, this is the general way we write down the directive. Hope from here you need to take over to convert to your task.

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

2 Comments

Editing my answer. Missed to add return in the directive function and right click and check the console.log you will see that element is picked.
What's the point of a creating a directive if you still use selectors and jQuery?
0

AngularJS comes with it's own stripped down version of jQuery called jQLite. You can use it like so:

angular.element(<selector>).<operation>

You can find more documentation here

NOTE: You don't normally need to use jQuery. While jQuery does work fine with Angular, you want to use more of an Angular style of development and DOM manipulation, using directives.

1 Comment

I know that , can you make me full answer,

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.