自定义过滤器无法在Angular Hybrid应用中使用

时间:2017-12-28 21:05:29

标签: javascript angularjs angular-filters angular5 multi-device-hybrid-apps

我正在尝试将AngularJS 1.6应用程序与Angular 5一起转换为混合应用程序。我定义了以下简单过滤器:

(function () {
    "use strict";
    var filterId = "colorPicker";

    angular
        .module('app')
        .filter('colorPicker', colorPicker);

    function colorPicker() {
        return function (input) {
            var colorCode = '#2980b9';
            switch (input) {
                case 1:
                    colorCode = '#16a085';
                    break;
                case 2:
                    colorCode = '#a38761';
                    break;
                case 3:
                    colorCode = '#8e44ad';
                    break;
                case 4:
                    colorCode = '#ffa800';
                    break;
                case 5:
                    colorCode = '#d95459';
                    break;
                case 6:
                    colorCode = '#8eb021';
                    break;
                default:
            }
            return colorCode;
        };
    }
})();

过滤器的使用方式如下:ng-attr-style="background-color: {{ $index | colorPicker }}"

这适用于AngularJS应用,但我在混合应用中遇到以下错误:angular.js:14525 Error: [$injector:unpr] Unknown provider: colorPickerFilterProvider <- colorPickerFilter

从AngularJS代码中调用过滤器,就像之前一样。事实上,我几乎没有任何Angular 5代码。我只是试图让现有代码运行,但在Hybrid应用程序中。我不应该像以前一样使用过滤器吗?

更新

我认为这可能与我得到的有关控制器的其他错误有关:

[$controller:ctrlreg] The controller with the name 'myController' is not registered.

我可以看到脚本文件下载成功,并且当app.module.ts引导AngularJS时没有抛出任何错误。事实上,我有点确定AngularJS正在运行,因为它收到了globalVars没有被注入的错误(这是在我不再使用的剃刀视图中定义的)但是错误在我之后消失了在TypeScript中重新创建它并“降级”它,以便AngularJS可以使用它。

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpClientModule } from '@angular/common/http';
import { FormsModule } from '@angular/forms';
import { downgradeInjectable, UpgradeModule } from '@angular/upgrade/static';

import { AppComponent } from './app.component';
import { GlobalVarsService } from './core/global-vars.service';

declare var angular: any;

angular.module('app', []).factory('globalVars', downgradeInjectable(GlobalVarsService));

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpClientModule,
    UpgradeModule
  ],
  providers: [
    GlobalVarsService
  ],
  bootstrap: [AppComponent]
})
export class AppModule {
  constructor(private upgrade: UpgradeModule) {
    this.upgrade.bootstrap(document.body, ['app'], { strictDi: true });
  }
}

因此文件正在下载并执行,但应用程序找不到控制器和过滤器(可能还有其他项目)。我将所有旧代码放在名为“old”的文件夹中,然后更新.angular-cli.json以在构建时将这些文件复制到输出中,以便我可以通过index.html中的<script>标记引用它们。这应该工作,不应该吗?或者出于某种原因,文件是否需要与Angular 5文件捆绑在一起?

// .angular-cli.json section
  "assets": [
    "assets",
    { "glob": "**/*", "input": "../old/app/", "output": "./app/" },
    { "glob": "**/*", "input": "../old/Content/", "output": "./Content/" },
    { "glob": "**/*", "input": "../old/Scripts/", "output": "./Scripts/" },
    "favicon.ico"
  ], 

1 个答案:

答案 0 :(得分:1)

发现问题。其实我觉得有两个问题。一个是我认为在降级我的globalVars服务时,我通过包含方括号来重新定义“app”。

angular.module('app', []).factory('globalVars', downgradeInjectable(GlobalVarsService));

而不是

angular.module('app').factory('globalVars', downgradeInjectable(GlobalVarsService));

我认为另一个问题是鸡蛋和鸡蛋的问题。我的globalVars被注入到我的AngularJS应用程序的配置功能中,但我认为应用可能需要降级globalVars - 仍然不完全确定。幸运的是,我的globalVars中没有任何内容引用app.js,因此我可以删除引用。

这是我app.module.ts的版本,最终让它发挥作用。我希望这有助于其他人!

import { NgModule, APP_INITIALIZER } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpClientModule } from '@angular/common/http';
import { HttpClient } from '@angular/common/http';
import { downgradeInjectable, UpgradeModule } from '@angular/upgrade/static';
import { environment } from '../environments/environment';

import { AppComponent } from './app.component';
import { GlobalVarsService } from './core/global-vars.service';

declare var angular: any;

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    HttpClientModule,
    UpgradeModule
  ],
  providers: [
    {
      provide: APP_INITIALIZER,
      useFactory: OnAppInit,
      multi: true,
      deps: [GlobalVarsService, HttpClient]
    },
    GlobalVarsService
  ]
})
export class AppModule {
  constructor(private upgrade: UpgradeModule, private http: HttpClient) { }
  ngDoBootstrap() {
    angular.module('app').factory('globalVars', downgradeInjectable(GlobalVarsService));
    this.upgrade.bootstrap(document.body, ['app'], { strictDi: true });
  }
}

export function OnAppInit(globalVars: GlobalVarsService, http: HttpClient) {
  return (): Promise<any> => {
    return new Promise((resolve, reject) => {
      // Fetch data from the server before initializing the app.
      http.get(environment.apiBase + '/api/meta/data').subscribe(x => {
        globalVars.MetaData = x;
        globalVars.VersionNumber = globalVars.MetaData.versionNumber;
        globalVars.IsDebugBuild = globalVars.MetaData.isDebugBuild;
        globalVars.AuthorizedFeatures = globalVars.MetaData.authorizedFeatures;
        globalVars.User = globalVars.MetaData.user;
        resolve();
      });
    });
  };
}