我一直在寻找这个问题的答案,乍一看,它似乎类似于angularJS的其他控制器注册问题,但对于我的特定用例还没有一个好的答案。
我正在对我们遗留的angularJS应用程序进行复杂的重构。第一步是将所有内容放入组件中,然后我们将开始升级到Angular 2+。
我的问题是主控制器。我已经将其转换为组件,详细如下:
const MainControllerComponent = {
selector: 'mainController',
templateUrl: '../../index.html',
bindings: {},
controller: class MainController {
constructor() {
...constructor stuff
}
}
angular
.module('app')
.component(MainControllerComponent.selector, MainControllerComponent);
字符串
使用此控制器的索引HTML如下:
<html lang="en" ng-app="app">
<body id="mainController" ng-controller="MainController as main" ng-class="{ 'is-mobile': $ctrl.isMobile, 'is-desktop': !$ctrl.isMobile, 'anon': !$ctrl.isUserAuthenticated, 'walkthrough': $ctrl.isWalkthrough(), 'root-controller': $ctrl.isRootController() }" class="{{bodyClass}}">
...Other body html stuff
</body>
</html>
型
应用程序编译(我们使用gulp/sass),但不会加载。我得到以下错误:
第一个月
在app.js中,我们使用require()来包含构建中的所有js/tsrequire('./js').default(ngModule);
个
controllers文件夹的索引如下:
export default ngModule => {
require('./activate_controller').default(ngModule); //Not yet converted
require('./activity_controller'); //converted and works
...
require('./login_controller').default(ngModule); //not yet converted
require('./main_controller'); //converted and won't register
...
};
型
所以我很困惑。我试过将ng-controller中的引用重命名为组件名。与这里的许多其他示例不同,应用名称是正确的,应用使用gulp编译。如果我删除ng-controller属性,应用加载,但所有主要功能都被破坏了(没有头/菜单等)但子控制器按预期工作。所以它似乎只是一个问题,让index.html看到新组件作为一个有效的控制器。
我在这里做错了什么?我甚至尝试在我们的router.js中添加一个虚拟条目(它为我们的应用程序定义了$stateProvider),但它什么也没做。
谢谢你,谢谢
1条答案
按热度按时间bttbmeg01#
mainController
在选择器中,但MainController
在ng-controller
中你们有弹壳差异。