Javascript AngularJs从控制器调用服务函数

Javascript AngularJs从控制器调用服务函数,javascript,angularjs,Javascript,Angularjs,我在AngularJs中有一些应用程序,我遇到了一个问题。我需要从控制器中的服务调用函数 我的服务: var DataService = ['$http', '$q', '$window', 'alert', function ($http, $q, $window, alert) { function print () { console.log('smth'); } } 我的控制器: var Controller = function ($scope, $s

我在AngularJs中有一些应用程序,我遇到了一个问题。我需要从控制器中的服务调用函数

我的服务:

var DataService = ['$http', '$q', '$window', 'alert', function ($http, $q, $window, alert) {
    function print () {
        console.log('smth');
    }
}
我的控制器:

var Controller = function ($scope, $state, OffersService, commonFunction, dataService, alert, blockUI) {
    function printSmth () {
        dataService.print();
    }
}
/* 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();
    };
}
函数printSmth是在html中从ng init调用的,我得到的异常是dataService.print不是一个函数

有人知道正确的方法吗?我不能把它改成服务。必须这样做

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

----------------更新--------------------

无法在视图中访问您在控制器中定义的函数,除非它是
$scope
函数

因此,使控制器中的打印功能

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

----------------更新--------------------

无法在视图中访问您在控制器中定义的函数,除非它是
$scope
函数

因此,使控制器中的打印功能

$scope.printSmth = function () {
    dataService.print();
}
试试下面的方法

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

试试下面的方法

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


实现目标的最佳方式是:

服务:

/* 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');
    }
}
控制器:

var Controller = function ($scope, $state, OffersService, commonFunction, dataService, alert, blockUI) {
    function printSmth () {
        dataService.print();
    }
}
/* 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();
    };
}

我建议你开始读一些。您将使您的生活和您的开发团队在未来更加高效。

实现目标的最佳方式是:

服务:

/* 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');
    }
}
控制器:

var Controller = function ($scope, $state, OffersService, commonFunction, dataService, alert, blockUI) {
    function printSmth () {
        dataService.print();
    }
}
/* 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();
    };
}

我建议你开始读一些。你将使你的生活和你的开发团队在未来更加高效。

不,我也得到了同样的例外,关于控制器中的这种想法,我可以在DataService中访问VAR看看@egzaellnope,我也得到了同样的例外,关于控制器中的这种想法,我可以在dataServiceHave look中访问变量@egzaellIt是一个使用一些良好实践编写的好例子。请参考这个。这是一个使用一些良好实践编写的好例子。请参考此。欢迎@egzaell欢迎@egzaell