15

I have below Javascript functions

<script type="text/javascript">
    function ShowProgress() {
        var modal = $('<div />');
        modal.addClass("spinmodal");
        $('body').append(modal);
        var loading = $(".loading");
        loading.show();
        var top = Math.max($(window).height() / 2 - loading[0].offsetHeight / 2, 0);
        var left = Math.max($(window).width() / 2 - loading[0].offsetWidth / 2, 0);
        loading.css({ top: top, left: left });
    }

    function HideProgress() {
        var loading = $(".loading");
        loading.hide();
        $(".spinmodal").remove();
    }
</script>

now I want to call this ShowProgress() and HideProgress() in Angular controller. I want to call ShowProgress() as soon as deletePrepared invoked and HideProgress() below GetAllPrepared.

<script type="text/javascript">
    app.controller("myCntrl", function ($scope, angularService, $modal) {

        $scope.deletePrepared = function (itm) {
            var getData = angularService.DeletePrepared(itm.ProductId);
            getData.then(function (msg) {
                GetAllPrepared();
            }, function () {
                alert('Error in Deleting Record');
            });
        }

    });
</script>
2
  • 1
    just call those mthods and see what is happening Commented Jul 3, 2015 at 3:10
  • 1
    The dom manipulation is better done in directive instead of controller. Commented Jul 3, 2015 at 3:20

2 Answers 2

23

Simply call those methods:

app.controller("myCntrl", function ($scope, angularService, $modal) {

    $scope.deletePrepared = function (itm) {
        ShowProgress();

        var getData = angularService.DeletePrepared(itm.ProductId);
        getData.then(function (msg) {
            HideProgress();
            GetAllPrepared();
        }, function () {
            alert('Error in Deleting Record');
        });
    }

});

Tip: Use Angular directives to do DOM manipulation and you don't require any jQuery code for DOM manipulation, Angular is sufficient for it.

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

Comments

0

You can create a service and plug it inside the controller. By this, you can reuse the function in multiple controllers.

Example,

/* Controller */
angular.module('appApp')
  .controller('myCtrl', function ($scope, Modal) {
    Modal.openModal("myBtn");
}

/* Service */
angular.module('appApp')
  .factory('Modal', function() {
    return {
      openModal: function(btnId) {
            // Java script code goes here...
      }
    };
});

P.S: Don't forget to add service in your index.html file.

I hope this helps!

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.