简体   繁体   English

AngularJs从控制器调用服务功能

[英]AngularJs calling service function from controller

I've got some app in AngularJs, and I've encountered a problem. 我在AngularJs中有一些应用程序,但遇到了问题。 I need to call a function from service in controller. 我需要从控制器中的服务调用函数。

my service: 我的服务:

var DataService = ['$http', '$q', '$window', 'alert', function ($http, $q, $window, alert) {
    function print () {
        console.log('smth');
    }
}

my controller: 我的控制器:

var Controller = function ($scope, $state, OffersService, commonFunction, dataService, alert, blockUI) {
    function printSmth () {
        dataService.print();
    }
}

function printSmth is called from ng-init in html, and I get exception saying that dataService.print is not a function. 从html中的ng-init调用了函数printSmth,并且出现异常,说dataService.print不是函数。

Does anybody know the proper way to do it? 有人知道这样做的正确方法吗? I can't change it to .service it must be done this way. 我无法将其更改为.service,必须以这种方式完成。

try like below.. 尝试如下。

var DataService = ['$http', '$q', '$window', 'alert', function ($http, $q, $window, alert) {
   this.print = function () {
        console.log('smth');
    };
}

or 要么

var DataService = ['$http', '$q', '$window', 'alert', function ($http, $q, $window, alert) {
       function print() {
            console.log('smth');
        };
       return {
        print: print
       };
}

The best way to what you want to accomplish would be something like: 达到目标的最佳方法是:

Service: 服务:

/* recommended */

// dataservice factory
angular
    .module('app.core')
    .factory('dataservice', dataservice);

dataservice.$inject = ['$http', '$q', '$window', 'alert'];

function dataservice($http, $q, $window, alert) {
    return {
        print : print 
    };

    function print () {
        console.log('smth');
    }
}

Controller: 控制器:

/* recommended */

// controller calling the dataservice factory
angular
    .module('app.avengers')
    .controller('YourController', YourController);

YourController.$inject = ['$scope', '$state', 'OffersService', 'commonFunction', 'dataservice', 'alert', 'blockUI'];

function YourController($scope, $state, OffersService, commonFunction, dataservice, alert, blockUI) {
    $scope.printSmth = function printSmth() {
           dataService.print();
    };
}

I recommend you start reading some style guides for AngularJS . 我建议您开始阅读style guides for AngularJS一些style guides for AngularJS You will make your life and your development team more productive in the future. 将来,您将使您的生活和开发团队更加高效。

var Controller = function ($scope, $state, OffersService, commonFunction, dataService, alert, blockUI) {

Change dataService to DataService dataService更改为DataService

----------------UPDATE-------------------- ----------------更新--------------------

The function you have defined in controller cannot be accessed in view unless its a $scope function. 除非有$scope函数,否则无法在视图中访问在控制器中定义的函数。

So make the print function in your controller to be 因此,将控制器中的打印功能设置为

$scope.printSmth = function () {
    dataService.print();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM