如何在AngularJS控制器中调用javascript函数?

时间:2022-05-05 13:42:54

I have below Javascript functions

下面是Javascript函数

<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.

现在我想在角度控制器中调用这个ShowProgress()和HideProgress()。只要在getallprepare下面的deleteprepare调用和HideProgress(),我就会调用ShowProgress()。

<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


21  

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.

提示:使用角化指令进行DOM操作,并且不需要任何jQuery代码进行DOM操作,角化就足够了。

#2


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.

P。别忘了在索引中添加服务。html文件。

I hope this helps!

我希望这可以帮助!

#1


21  

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.

提示:使用角化指令进行DOM操作,并且不需要任何jQuery代码进行DOM操作,角化就足够了。

#2


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.

P。别忘了在索引中添加服务。html文件。

I hope this helps!

我希望这可以帮助!