1

I want to add jQuery datatable using AngularJS.

 $scope.SearchData = function () {
        var myData = new Object();
        myData.EmployeeCode = $("#Person").val();


        $http.post("/admin/GetData", myData)
            .then(function (response) {
                $scope.itemData = response.data.Initiator;
                $('#dtBasicExample').DataTable();
            });
    };

I am using this searching the data. This is working after the second click of button.

0

1 Answer 1

1

i think it's watchers problem, use setTimeout and apply to fix it:

 myApp.controller('myController', function($scope, $timeout){ // just example

  $timeout(()=>{
     $('#dtBasicExample').DataTable();
   })

or

 setTimeout(()=>{
     $('#dtBasicExample').DataTable();
     $scope.$apply(); //this triggers a $digest
   })

Angular has no way of knowing what you might change. In this case it’s your responsibility to call $apply() manually, which triggers a $digest cycle.

Similarly, if you have a directive that sets up a DOM event listener and changes some models inside the handler function, you need to call $apply() to ensure the changes take effect.

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

3 Comments

There is no need of setTimeout in that case, and if there was then angular has $timeout service which is better to use.
@BillP, you are right, $timeout replace setTimout with $apply
There's $setTimeout in your first snippet instead of $timeout

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.