如何在angularjs中调用控制器内的javascript函数

时间:2022-03-15 16:58:04

I have a code snippet below.

我下面有一个代码片段。

sample.js

    (function() {
    /*global angular */
    'use strict';

    angular.module('myapp', ['spinner'])
       .controller('myCtrl', ['$scope', '$window', function ($scope, $window ) {

    $scope.methodname = function() {
            if(something){
                /* Doing some operations */
            }
    };
    /* Here I need to define the callme javascript function */

   function callme(response){
       /* If I call like this, I'm getting error in console. */
    }

    }]);  /* Controller ends here */

    /* Creating a new anonymous function to perform some operations */
    (function () {
    'use strict';


     /* Edited */
   code.util.myHTTP(url, function (response) {

             // Adding response to session storage
            callme(response);

             }, function () {
               // Removing from session storage
        });

        })();
     }());

Here, I can't able to call callme javascript function inside angular controller . I'm getting error in console like

在这里,我无法在角度控制器内调用callme javascript函数。我在控制台中遇到错误

Uncaught ReferenceError: callme is not defined

Is there any way to achieve this?

有没有办法实现这个目标?

Edit:

I need to use some controller parameters inside callme function, that's why I'm defining callme function inside controller.

我需要在callme函数中使用一些控制器参数,这就是我在控制器中定义callme函数的原因。

I have run function in my js file already like below

我已经在我的js文件中运行函数,如下所示

.run(function($rootScope, $log, $window) {
});

How should I append myCtrl here?

我应该如何在这里添加myCtrl?

1 个解决方案

#1


1  

The dirty way

肮脏的方式

First, if you want to use your your callmecontroller function, then you have to expose it. As you wrote it, it's still private. To make it public, just "append" it to your controller's scope (just as you did with scope.methodname) :

首先,如果你想使用你的callmecontroller函数,那么你必须公开它。正如你所写,它仍然是私密的。要将其公开,只需将其“附加”到控制器的范围(就像使用scope.methodname一样):

...
$scope.callme = function(){
...
}
..

Then, use this function in a module so that the controller could be reachable :

然后,在模块中使用此功能,以便可以访问控制器:

angular.module('amodule').run(['myCtrl ', function(myCtrl){
    myCtrl.callme();
}]);

Another way

The best thing to do is to use a factory as you want to share a service :

最好的办法是使用工厂来共享服务:

angular.module('myapp').factory('myservice', function(){
    function callme(){
        // TODO : implement the service
    }
    return {
        callme:callme
    };
});

Then in a new module, call that method :

然后在新模块中,调用该方法:

angular.module('amodule').run(['myservice ', function(myservice){
    myservice.callme();
}]);

If you want to call that service outside angular (as you are wanting to do):

如果你想在角度以外调用该服务(如你所愿):

angular.injector(['myservice']).get('callme').call();

Edit : You can declare inject the service or controller in one run. It will work. Please, just keep in mind that injecting your controller in your module's run method is a result of bad design. Use factories/services to share data/services. I'm sure that with more code we can help your more.

编辑:您可以在一次运行中声明注入服务或控制器。它会工作。请记住,在模块的运行方法中注入控制器是设计糟糕的结果。使用工厂/服务来共享数据/服务。我相信,通过更多代码,我们可以为您提供更多帮助。

#1


1  

The dirty way

肮脏的方式

First, if you want to use your your callmecontroller function, then you have to expose it. As you wrote it, it's still private. To make it public, just "append" it to your controller's scope (just as you did with scope.methodname) :

首先,如果你想使用你的callmecontroller函数,那么你必须公开它。正如你所写,它仍然是私密的。要将其公开,只需将其“附加”到控制器的范围(就像使用scope.methodname一样):

...
$scope.callme = function(){
...
}
..

Then, use this function in a module so that the controller could be reachable :

然后,在模块中使用此功能,以便可以访问控制器:

angular.module('amodule').run(['myCtrl ', function(myCtrl){
    myCtrl.callme();
}]);

Another way

The best thing to do is to use a factory as you want to share a service :

最好的办法是使用工厂来共享服务:

angular.module('myapp').factory('myservice', function(){
    function callme(){
        // TODO : implement the service
    }
    return {
        callme:callme
    };
});

Then in a new module, call that method :

然后在新模块中,调用该方法:

angular.module('amodule').run(['myservice ', function(myservice){
    myservice.callme();
}]);

If you want to call that service outside angular (as you are wanting to do):

如果你想在角度以外调用该服务(如你所愿):

angular.injector(['myservice']).get('callme').call();

Edit : You can declare inject the service or controller in one run. It will work. Please, just keep in mind that injecting your controller in your module's run method is a result of bad design. Use factories/services to share data/services. I'm sure that with more code we can help your more.

编辑:您可以在一次运行中声明注入服务或控制器。它会工作。请记住,在模块的运行方法中注入控制器是设计糟糕的结果。使用工厂/服务来共享数据/服务。我相信,通过更多代码,我们可以为您提供更多帮助。