角度:service.setData不是一个函数 [英] Angular: service.setData is not a function

查看:38
本文介绍了角度:service.setData不是一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么angular告诉我setData不是函数?

Why angular says me that setData is not a function?

angular.module('mdl').factory('DataService', ['$http', '$cookieStore', '$rootScope',
    function ($http, $cookieStore, $rootScope) {
        return {
            setData: function (data) {
                $rootScope.data = data;
            },
        };
    }
]);

这是我的控制器,它调用 setData .

Here is my controller which calls setData.

angular.module('mdl').controller('DataCtrl', ['$scope', '$http', '$location', '$rootScope', 'DataService',
function($scope, $http, $location, DataService) {

    $scope.getData = function (id) {
        $http.post('/rest/data/get', id)
            .success(function (data, status, headers, config) {
                DataService.setData(data);
                $location.path('/main');
            })
            .error(function (data, status, headers, config) {

            });
    };
}

]);

推荐答案

依赖项注入存在问题,您声明了6个项目,而在函数中则有5个项目.缺少的是DataService.

There is a problem on dependencies injection, you are declaring 6 items and in your function you have 5 items. And the missing is DataService.

['$scope', '$http', '$location', '$rootScope', 'DataService',
function($scope, $http, $location, $rootScope /*Missing*/, DataService) {
…
}

这篇关于角度:service.setData不是一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆