角:在供应商使用$ HTTP [英] Angular: Using $http in a provider

查看:107
本文介绍了角:在供应商使用$ HTTP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建这是露出了的getSession 方法,我需要再进一个特定的路径来解决的角度提供者:

  VAR服务ID =会话;。angular.module(应用程序)供应商(服务ID,sessionProvider);功能sessionProvider(){    this.session =不确定;    这一点。$ GET =功能($ HTTP){
        返回{
            的getSession:功能(){
                返回$ http.get(/会话信息)
                        。然后(_onSuccess,_onError);
            }
        }
    }    功能_onSuccess(数据){
        返回数据。数据;
    }    功能_onError(数据){
        返回的数据;
    }
}

我再注入到我的配置,并尝试使用它:

  angular.module(应用程序)。配置([
            $ routeProvider,$ httpProvider,sessionProvider,功能
                ($ routeProvider,$ httpProvider,sessionProvider){
                $ routeProvider.when(/,{
                    templateUrl:应用程序/家/ /home.html的,
                    解析:{
                        会议:sessionProvider.getSession()
                    }
                });
            }
    ]);

不过,我得到以下错误:

 未捕获的错误:[$喷油器:modulerr]未能实例由于模块的应用程序:
    类型错误:未定义不是一个函数

我不知道我做了什么错在这里?

修改

当我尝试@DrogoNevets如下回答:

 函数sessionProvider(){    this.session =不确定;    这一点。$ GET =功能($ HTTP){
        返回{
        }
    }    this.getSession =功能(){
        返回$ http.get(/会话信息)
                    。然后(_onSuccess,_onError);
    }    功能_onSuccess(数据){
        this.session =数据。数据;
        返回数据。数据;
    }    功能_onError(数据){
        返回的数据;
    }
}

我碰到下面的错误,我也不是能够注入 $ HTTP 进入供应商本身是正确的?

 未捕获的错误:[$喷油器:modulerr]未能实例由于模块的应用程序:
    的ReferenceError:$ HTTP是没有定义


解决方案

如果您想从配置部分调用的getSession你需要将其分配到这个未在 $ GET 部分

尝试是这样的:

 函数sessionProvider(){
    VAR会议;    这一点。$ GET =功能(){
        返回{
            的getSession:this.getSession,
            setSession:this.setSession
        }
    }    功能_onSuccess(数据){
        返回数据。数据;
    }    功能_onError(数据){
        返回的数据;
    }
    this.getSession =功能(){
        回会议
    }    this.setSession =功能(ID){
        会话= ID;
    }
}

下面是小提琴展示我的意思: http://jsfiddle.net/EQ86N/1/

I've created an angular Provider which is exposing a getSession method which I need to resolve before going into a specific route:

var serviceId = 'session';

angular.module("app").provider(serviceId, sessionProvider);

function sessionProvider() {

    this.session = undefined;

    this.$get = function($http) {
        return {
            getSession: function () {
                return $http.get("/session-info")
                        .then(_onSuccess, _onError);
            }
        }
    }

    function _onSuccess(data) {
        return data.data;
    }

    function _onError(data) {
        return data;
    }
}

I then inject it into my config and try to use it:

angular.module("app").config([
            "$routeProvider", "$httpProvider", "sessionProvider", function 
                ($routeProvider, $httpProvider, sessionProvider) {
                $routeProvider.when("/", {
                    templateUrl: "app/home/home.html",
                    resolve: {
                        session: sessionProvider.getSession()
                    }
                });
            }
    ]);

But I get the following error:

Uncaught Error: [$injector:modulerr] Failed to instantiate module app due to:
    TypeError: undefined is not a function

I'm not sure what I've done wrong here?

Edit

When I try @DrogoNevets answer below:

function sessionProvider() {

    this.session = undefined;

    this.$get = function ($http) {
        return {
        }
    }

    this.getSession = function () {
        return $http.get("/session-info")
                    .then(_onSuccess, _onError);
    }

    function _onSuccess(data) {
        this.session = data.data;
        return data.data;
    }

    function _onError(data) {
        return data;
    }
}

I get the following error as I'm not able to inject $http into the provider itself, is that correct?

Uncaught Error: [$injector:modulerr] Failed to instantiate module app due to:
    ReferenceError: $http is not defined

解决方案

if you want to call getSession from the config part you need to assign it to this not within the $get part

try something like this:

function sessionProvider() {
    var session;

    this.$get = function() {
        return {
            getSession: this.getSession,
            setSession: this.setSession
        }
    }

    function _onSuccess(data) {
        return data.data;
    }

    function _onError(data) {
        return data;
    }


    this.getSession= function () {
        return session
    }

    this.setSession= function (id) {
        session = id;
    }
}

here is a fiddle showing what I mean: http://jsfiddle.net/EQ86N/1/

这篇关于角:在供应商使用$ HTTP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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