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

问题描述 投票:2回答:1

我试图将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"
  ], 
javascript angularjs angular-filters angular5 multi-device-hybrid-apps
1个回答
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();
      });
    });
  };
}
© www.soinside.com 2019 - 2024. All rights reserved.