我正在尝试将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"
],
最佳答案
找到了问题。实际上,我认为有两个问题。一个是我认为我在降级globalVars
服务时通过在方括号中重新定义“ app”。angular.module('app', []).factory('globalVars', downgradeInjectable(GlobalVarsService));
代替angular.module('app').factory('globalVars', downgradeInjectable(GlobalVarsService));
我认为另一个问题是鸡和蛋的问题。我的globalVars
被注入到AngularJS应用程序的config函数中,但我认为可能需要降级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();
});
});
};
}