Angular.js call jquery function in Directive

ftdeveloper

In my app i am trying to call $('#accountTable').dataTable(); this function in my controller. But I think it doesnt work like that in angular.js. Tried to call this function in my Directive but i did not work.

My Directive:

'use strict'

app.directive('dataTableDirective', function () {
    return {
        restrict: "A",
        link: function (scope, elem, attrs) {
            $('#accountTable').dataTable();
        }
    }
});

Chev

Angular uses JQuery under the hood if you have JQuery referenced. If you don't then it falls back on a slimmer version of JQuery called JQuery Lite. The elem argument to the link function is already a JQuery wrapped object representing the element your directive is attached to. Just call the plugin from there and it should work fine. It is best to avoid the classic JQuery selectors to navigate the DOM and instead lean on Angular to provide the elements you need.

Make sure you have JQuery referenced before Angular in your script references.

app.directive('dataTableDirective', function () {
  return {
    restrict: "A",
    link: function (scope, elem, attrs) {
      elem.dataTable();
    }
  };
});

Angular needs to know about changes when they happen. If you assign any events and need to update scope variables, you'll need to make sure that Angular knows about those changes by wrapping them in scope.$apply. For example:

app.directive('dataTableDirective', function () {
  return {
    restrict: "A",
    link: function (scope, elem, attrs) {
      elem.on('order.dt', function (e) {
        scope.something = 'someValue';
      }).dataTable();
    }
  };
});

The above code will set the something property on scope, but because the event was triggered outside of an Angular digest cycle, any UI bound to the something variable may not appear to update. Angular needs to be told of the change. You can make sure the change happens during a digest cycle like this:

app.directive('dataTableDirective', function () {
  return {
    restrict: "A",
    link: function (scope, elem, attrs) {
      elem.on('order.dt', function (e) {
        scope.$apply(function () {
          scope.something = 'someValue';
        });
      }).dataTable();
    }
  };
});

Then in your markup:

<table data-data-table-directive>
  <!-- table contents -->
</table>

@supr pointed this out in the comments. Note that the attribute is data-data-table-directive not data-table-directive. There is an HTML convention that you can begin arbitrary attributes with data- and Angular respects that and omits it. For example, you can put ng-click on an element or you can put data-ng-click on an element and they would both work the same. It also supports x-ng-click as another convention.

This is super relevant to you because it just so happens that your directive name begins with the word "data", so you'll need to double up on the data- in the beginning. Hopefully that makes sense.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Angular JS: How to call a function inside link from controller in angular directive

분류에서Dev

AngularJS set synchronous call of function from directive

분류에서Dev

angular directive - scope undefined inside function

분류에서Dev

Define a function for ng-click in an Angular directive

분류에서Dev

Angular directive link function never runs

분류에서Dev

JQuery not detecting DOM elements inside angular directive

분류에서Dev

Angular.js directive isolated scope not working

분류에서Dev

Angular.js directive isolated scope not working

분류에서Dev

Creating custom angular directive for term.js

분류에서Dev

Reload directive method after POST in Angular JS

분류에서Dev

jQuery validate dependancy call function

분류에서Dev

jquery second function call not working

분류에서Dev

Prevent Angular JS Directive from Firing Twice when moving element

분류에서Dev

Chosen.js doesn't work well with Angular directive

분류에서Dev

Call function in nodejs from angular application

분류에서Dev

Use Angular in javascript call function parameters

분류에서Dev

Call function within autocomplete jQuery plugin

분류에서Dev

Call jquery function when <ul> content change

분류에서Dev

Jquery ajax call inside success function

분류에서Dev

why cannot recursively call function in jquery?

분류에서Dev

jQuery - how to call method/prototype function

분류에서Dev

How call a Jquery function in the OnClick event?

분류에서Dev

cant call a javascript function within jquery

분류에서Dev

How jquery can call a function in the 'end' of another?

분류에서Dev

function inside for loop (js/jquery)

분류에서Dev

How do I call a jQuery function on several jQuery objects?

분류에서Dev

Updates controller-value and call controller-function from a directive. controller-calue not updated before function is called

분류에서Dev

Update data in angular directive

분류에서Dev

Angular directive modifying scope

Related 관련 기사

  1. 1

    Angular JS: How to call a function inside link from controller in angular directive

  2. 2

    AngularJS set synchronous call of function from directive

  3. 3

    angular directive - scope undefined inside function

  4. 4

    Define a function for ng-click in an Angular directive

  5. 5

    Angular directive link function never runs

  6. 6

    JQuery not detecting DOM elements inside angular directive

  7. 7

    Angular.js directive isolated scope not working

  8. 8

    Angular.js directive isolated scope not working

  9. 9

    Creating custom angular directive for term.js

  10. 10

    Reload directive method after POST in Angular JS

  11. 11

    jQuery validate dependancy call function

  12. 12

    jquery second function call not working

  13. 13

    Prevent Angular JS Directive from Firing Twice when moving element

  14. 14

    Chosen.js doesn't work well with Angular directive

  15. 15

    Call function in nodejs from angular application

  16. 16

    Use Angular in javascript call function parameters

  17. 17

    Call function within autocomplete jQuery plugin

  18. 18

    Call jquery function when <ul> content change

  19. 19

    Jquery ajax call inside success function

  20. 20

    why cannot recursively call function in jquery?

  21. 21

    jQuery - how to call method/prototype function

  22. 22

    How call a Jquery function in the OnClick event?

  23. 23

    cant call a javascript function within jquery

  24. 24

    How jquery can call a function in the 'end' of another?

  25. 25

    function inside for loop (js/jquery)

  26. 26

    How do I call a jQuery function on several jQuery objects?

  27. 27

    Updates controller-value and call controller-function from a directive. controller-calue not updated before function is called

  28. 28

    Update data in angular directive

  29. 29

    Angular directive modifying scope

뜨겁다태그

보관