Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AngularJS服务-在控制器中使用_Angularjs_Angularjs Scope_Angularjs Service_Angularjs Controller - Fatal编程技术网

AngularJS服务-在控制器中使用

AngularJS服务-在控制器中使用,angularjs,angularjs-scope,angularjs-service,angularjs-controller,Angularjs,Angularjs Scope,Angularjs Service,Angularjs Controller,我正在构建一个非常简单的应用程序,其中有一个GlobalController(在body元素上),下面还有一个子控制器。这是一个有多个物理页面的模板站点,这样子控制器就不同了,但最多只有一个顶级全局页面和一个子页面 我试图创建全局函数,任何子控制器都可以使用这些函数来运行每个子控制器都需要运行的代码,而不必在每个子控制器中复制这些函数 我可以这样做的一种方法是包含$rootScope,然后使用$on()向正在侦听它们的GlobalController发出()消息 我想这不是一个“好”的方法。相反

我正在构建一个非常简单的应用程序,其中有一个GlobalController(在body元素上),下面还有一个子控制器。这是一个有多个物理页面的模板站点,这样子控制器就不同了,但最多只有一个顶级全局页面和一个子页面

我试图创建全局函数,任何子控制器都可以使用这些函数来运行每个子控制器都需要运行的代码,而不必在每个子控制器中复制这些函数

我可以这样做的一种方法是包含$rootScope,然后使用$on()向正在侦听它们的GlobalController发出()消息

我想这不是一个“好”的方法。相反,我已经了解到,最好使用服务来实现这一点。我现在被困在如何实现这项服务上

我目前有一个全局控制器,如下所示:

var globalModule = angular.module('DoSquareStuff', ["ngRoute","list", "savings-video"]); 
        // there will be a long list of modules that will be added after "savings-video"


globalModule.factory('squareMgr', function() {
    var squares = SUYS.squares;  // global obj with earned[] and placed[]

    return {
        getSquaresEarned: function() {
            return squares.earned;
        },
        getSquaresPlaced: function() {
            return squares.placed;
        },
        setThisSquareEarned: function(value) {
            squares.earned.push(value);
        },
        setThisSquarePlaced: function(value) {
            squares.placed.push(value);
        },
        earnedThisSquare: function(squareNum) {
            return ~squares.earned.indexOf(squareNum);
        },
        placedThisSquare: function(squareNum) {
            return ~squares.placed.indexOf(squareNum);
        }
    }
});

globalModule.controller('GlobalController', function($scope, $squareMgr){ 
     // this would be easy... but it doesn't work


     //  $rootScope.$on('signal.missionComplete', function (event, missionId) {
     //       console.log("parentScope receive notice that mission " + missionId + " is complete.");
     //  });

     log('GlobalController loaded');
     //  log($squareMgr.getSquaresEarned()); //broken
});
然后,在阅读文档时,我尝试:

globalModule.controller('GlobalController', ['squareMgr', function($squareMgr){  

    // but then how do I get $scope in there?

    log('GlobalController loaded');
    //  log($squareMgr.getSquaresEarned());

}]);

在最后一个代码块中,还需要注入$scope。您可以插入所需的任意数量的服务:

globalModule.controller('GlobalController', ['squareMgr', '$scope', 
    function($squareMgr, scope){  

    // but then how do I get $scope in there?

    log('GlobalController loaded');
    //  log($squareMgr.getSquaresEarned());

}]);
还有一个小问题,我不会在squareMgr前面放一个
$
,这个
$
意味着它是一个内置的角度服务。

试试看

globalModule.controller('GlobalController', ['squareMgr', '$scope', function($scope, squareMgr){ .....

$符号用于区分Angular services和您自己的

数组中字符串的顺序需要与函数中参数的顺序相匹配。谢谢,但是,我应该在控制器中使用$scope吗?当然,在控制器中几乎总是使用$scope。它是Angular的核心部分。