Angular 1 - 用于检测更改redux状态更改的服务

时间:2017-04-13 09:22:44

标签: angularjs angular-services angularjs-injector

我正在尝试创建一个服务,该服务将检测应用程序的redux状态的更改,以便能够重定向到不同的页面。

我得到的错误是:

Error: [$injector:unpr] Unknown provider: translate2FilterProvider <- 
translate2Filter

http://errors.angularjs.org/1.6.4/ $注射器/ unprp0 = translate2FilterProvider%20%3 C-%20translate2Filter

这是我的重定向服务:

import * as angular from 'angular'
import {
    IStateService,
} from 'angular-ui-router'
import {subscribe} from 'awesomeLibrary_web_stateSubscribe'
import {getUser} from '../redux/get'
import {
    INgRedux,
    State,
} from '../redux/redux'
import {RouterParams} from '../redux/root/router/routerReducers'


export class RedirectionService {

    constructor(
        public  $ngRedux:       INgRedux,
        public  $stateParams:   RouterParams,
        public  $state:         IStateService,
        $scope:                 angular.IScope,
    ) {

        $scope.$on('$destroy', this.$ngRedux.connect(s => this._mapState(s))(this))

        $ngRedux.subscribe(async () => { await this.redirectinDetection() })

    }

    public async redirectinDetection() {

        const state = this.$ngRedux.getState()
        const user = getUser(state)

        if (this.$state.is('site.user')) {
            // Here it will redirect to user/:userId
            if (user && !this.$stateParams.userId) {
                this.$state.go('site.user', {userId: user.id})
            }
            else if (!user) {
                // not logged in
            }
        }

    }

    private _mapState = (state: State) => ({
        user: getUser(state),
    })

}

angular.module('Site')
    .service('redirectionSerivce', RedirectionService)

下面是我尝试将redirectionService注入其中的组件。它是主要的顶级组件,因为我希望此服务在整个站点上运行:

import * as es6Promise from 'es6-promise'
es6Promise.polyfill()

import * as angular from 'angular'
import 'angular-animate'
import 'angular-aria'
import 'angular-local-storage'
import 'angular-material'
import 'angular-messages'
import 'angular-ui-router'
import 'ng-redux'
import {
    INgRedux,
    State,
} from './redux/redux'
import {RouterParams} from './redux/root/router/routerReducers'
import '../../node_modules/angular-elastic/elastic'
import '../../node_modules/angular-material/angular-material.min.css'
import './cUser/cUser'
import './mConfig/persistentQueryParamsConfig'
import './mConfig/routeConfig'
import './mDirectives/mDirectives'
import './mServices/mServices'
import './redux/redux'

class SiteController {

    constructor(
        $ngRedux: INgRedux,
    ) {
        $ngRedux.connect(s => this._mapState(s))(this)
    }

    private _mapState = (state: State) => ({
        metaTitle:          'title'),
    })

}

class StatelessController {

    constructor(
        public $stateParams: RouterParams,
    ) {}

}

angular.module('Site').controller('SiteController', SiteController)
angular.module('Site').controller('StatelessController', StatelessController)
angular.module('Site').run([
    'redirectionService',
    angular.noop, // tslint:disable-line:no-unbound-method
])

1 个答案:

答案 0 :(得分:0)

你正在尝试注入一个名为translate2Filter的服务(可能是你所包含的这两部分代码),而这个服务还没有找到

检查html中是否有类似的过滤器,或查看所有项目以查找此过滤器的调用位置。

此致

PabloGarcía