加载控制器时出错-将AngularJS1迁移到Angular6 [英] Error while loading controllers - migrating AngularJS1 to Angular6

查看:76
本文介绍了加载控制器时出错-将AngularJS1迁移到Angular6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将用AngularJS1编写的组件升级到Angular6.在我的示例中,我通过扩展放置在文件夹" directive-wrappers "下的" UpgradeComponent ",为所有现有的AngularJS1组件提供了包装器.当我尝试添加一些没有任何指令的控制器时,出现错误消息

I'm trying to do upgrade components written in AngularJS1 to Angular6. I'm taking the approach of having the wrappers for all the existing AngularJS1 component by extending "UpgradeComponent" placed under the folder "directive-wrappers" in my example. When I try to add some controllers which doesn't have any directives, I get the error message

Error: [$injector:unpr] Unknown provider: testDirective2DirectiveProvider <- testDirective2Directive
https://errors.angularjs.org/1.7.8/$injector/unpr?p0=testDirective2DirectiveProvider%20%3C-%20testDirective2Directive
    at eval (angular.js:138)
    at eval (angular.js:4924)
    at Object.getService [as get] (angular.js:5084)
    at eval (angular.js:4929)
    at Object.getService [as get] (angular.js:5084)
    at Function.UpgradeHelper.getDirective (upgrade_helper.ts:56)
    at new UpgradeHelper (upgrade_helper.ts:52)
    at TestDirective2Wrapper.UpgradeComponent (upgrade_component.ts:106)
    at new TestDirective2Wrapper (TestDirective2Wrapper.ts:27)
    at createClass (provider.ts:265) "<app-root _nghost-c69="">"

我尝试添加studentController和homePageController,但无法加载它.有什么想法为什么我要面对这个问题吗? https://stackblitz.com/edit/ng6hybrid-c8h6uv

I tried adding studentController and homePageController, but not able to load it. Any ideas why I'm facing this issue? https://stackblitz.com/edit/ng6hybrid-c8h6uv

推荐答案

有两个问题需要解决.首先,在studentController.js文件中,您使用的是 var mainApp = angular.module("testApp",[]); 语法,该语法将重置AngularJS模块,并且之前已实例化的所有内容删除.

There are two issues that need to be addressed. First, in the studentController.js file, you are using the var mainApp = angular.module("testApp", []); syntax which is resetting the AngularJS module and everything that got instantiated before is being removed.

要回答有关控制器的问题,不能使用 UpgradeComponent 直接升级AngularJS控制器(请参阅

To answer your question about the controllers, AngularJS controllers can't be upgraded directly using UpgradeComponent (see https://angular.io/guide/upgrade#using-angularjs-component-directives-from-angular-code) because they aren't components/directives that have a template. That is why the error you get (after fixing the first issue) is Unknown provider: studentControllerDirectiveProvider <- studentControllerDirective. It can't find a directive for the reference studentController, so it is adding Directive to see if that has anything.

要解决此问题,请将AngularJS中的控制器转换为带有模板的指令,或将其转换为AngularJS中的服务,然后使用 UpgradeModule 将其注入Angular.请记住,您将在此控制器的某个位置需要一个模板,以便可以将其升级到Angular.请在下面的堆叠闪电战中查看此示例.

To fix this, either convert the controller in AngularJS to a directive with a template, or convert it to a service in AngularJS and then inject it into Angular using the UpgradeModule. Keep in mind you will need a template somewhere for this controller so it can be upgraded to Angular. Please see a working example of this in my stackblitz below.

https://stackblitz.com/edit/ng6hybrid-zs6waw?file=app%2Fdirective-wrappers%2FstudentControllerWrapper.ts

这篇关于加载控制器时出错-将AngularJS1迁移到Angular6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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