从服务变为控制器变量

时间:2014-06-30 04:33:45

标签: javascript angularjs yeoman

我试图从这项服务获得一个变化,但我无法做到这一点。我已经在网上看了一下,什么也没得到。服务:

    .factory('localStorageService', function($rootScope){
    $rootScope.go = function(task) {
        var dataReceiver = localStorage.getItem('taskData');
        var array = [];
        var newArray = array.push({"title" : task});

        if(dataReceiver === null) {
            localStorage.setItem("taskData", JSON.stringify(array));

            dataReceiver = JSON.parse(localStorage.getItem('taskData'));
            console.log(dataReceiver);
        }
        else {
            array = JSON.parse(dataReceiver);
            var newArray2 = array.push({"title" : task});
            localStorage.setItem("taskData", JSON.stringify(array));

            dataReceiver = JSON.parse(localStorage.getItem('taskData'));

            for(var i = 0; i < newArray2; i++) {
                console.log(dataReceiver[i].title);
            }   
        }

        return dataReceiver;
    }
})

控制器:

.controller('PlaylistsCtrl', function($scope, PlaylistService, localStorageService) {

console.log(localStorageService.go());

})

它说它无法读取未定义的属性

3 个答案:

答案 0 :(得分:1)

您需要使用工厂所属的模块声明,这就是您的控制器中未定义localStorageService的原因。是的,你也不应该追加到rootScope。你的工厂应该是这样的:

angular.module('myApp', []).factory('localStorageService', function($rootScope){

this.go = function(task) {
    var dataReceiver = localStorage.getItem('taskData');
    var array = [];
    var newArray = array.push({"title" : task});

    if(dataReceiver === null) {
        localStorage.setItem("taskData", JSON.stringify(array));

        dataReceiver = JSON.parse(localStorage.getItem('taskData'));
        console.log(dataReceiver);
    }
    else {
        array = JSON.parse(dataReceiver);
        var newArray2 = array.push({"title" : task});
        localStorage.setItem("taskData", JSON.stringify(array));

        dataReceiver = JSON.parse(localStorage.getItem('taskData'));

        for(var i = 0; i < newArray2; i++) {
            console.log(dataReceiver[i].title);
        }   
    }

    return dataReceiver;
};

return this;

});

通过返回工厂对象,当注入到控制器中时,它的所有方法都可用。

答案 1 :(得分:0)

你的问题是你把方法'go'放在了rootcope而不是工厂。

您的工厂正在返回没有方法“go”的对象dataReceiver。

将'go'方法放在从工厂返回的对象上,你就可以得到它。

这是一个基本的傻瓜:http://plnkr.co/edit/wbCedJx8BJ4nOvuNWQc7

JavaScript的:

var app = angular.module('app', []);

app.controller('appCtrl', function ($scope, myFactory) {

    $scope.greetings = myFactory.go();

});

app.factory('myFactory', function () {

  this.go = function () {
    return 'hello from factory';
  };

  return this;
});

标记:

<html ng-app="app">

  <head>
    <script data-require="angular.js@*" data-semver="1.3.0-beta.5" src="https://code.angularjs.org/1.3.0-beta.5/angular.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

  <body ng-controller="appCtrl">
    <h1>Greetings: {{ greetings }}</h1>
  </body>

</html>

希望这有帮助。

答案 2 :(得分:0)

服务

app.factory('localStorageService', ['$rootScope', function($rootScope) {
    return {
        go: function() {
            //do your stuff here
            var dataReceiver = localStorage.getItem('taskData');
            var array = [];
            var newArray = array.push({"title" : task});

            if(dataReceiver === null) {
                localStorage.setItem("taskData", JSON.stringify(array));

                dataReceiver = JSON.parse(localStorage.getItem('taskData'));
                console.log(dataReceiver);
            }
            else {
                array = JSON.parse(dataReceiver);
                var newArray2 = array.push({"title" : task});
                localStorage.setItem("taskData", JSON.stringify(array));

                dataReceiver = JSON.parse(localStorage.getItem('taskData'));

                for(var i = 0; i < newArray2; i++) {
                    console.log(dataReceiver[i].title);
                }   
            }

            return dataReceiver;
        }
    }
}]);

控制器

app.controller('PlaylistsCtrl',
    function PlaylistsCtrl($scope, PlaylistService, localStorageService) {
        console.log(localStorageService.go())
    }
)