从工厂Angularjs调用函数

时间:2015-08-03 20:54:22

标签: angularjs parameter-passing angularjs-factory

还是比较新的。我正在尝试从工厂获取和设置值,但我似乎无法运行函数。它调用类型错误。我也无法在没有杀死它的情况下将$ scope添加到工厂。帮助

app.factory('globals', function() {
    var globalTag = {};
        globalTag.currDrive = "";
        globalTag.myFaults = "";
    var availDrives = "";

    globalTag.currDrive = "this sucks";
    globalTag.myFaults = "I've got a lovely bunch of faulty faults";

    function setDrive(driveID) {
        globalTag.currDrive = driveID;
    };

    return globalTag;
});

app.controller('FaultController', ['$scope', '$routeParams', 'globals', function($scope, $routeParams, globals) {
    //Setup dummy paramenters
    $scope.driveInfo = {};
    $scope.driveInfo = globals.setDrive("350");
}]);

1 个答案:

答案 0 :(得分:1)

您永远不会从服务中公开setDrive方法。正确的代码是:

app.factory('globals', function () {
    var globalTag = {};
    globalTag.currDrive = "";
    globalTag.myFaults = "";
    var availDrives = "";

    globalTag.currDrive = "this sucks";
    globalTag.myFaults = "I've got a lovely bunch of faulty faults";

    function setDrive(driveID) {
        globalTag.currDrive = driveID;
    };

    globalTag.setDrive = setDrive; // <--- make it part of globals service API

    return globalTag;
});