简体   繁体   English

变更检测在降级的Angular组件中不起作用

[英]Change detection not working in downgraded Angular components

I'm in the process of converting a rather big application written in AngularJS to an Angular application using the @angular/upgrade module. 我正在使用@ angular / upgrade模块将用AngularJS编写的相当大的应用程序转换为Angular应用程序。 For now, every component from AngularJS is running as expected in the Angular app. 目前,AngularJS中的每个组件都可以在Angular应用程序中按预期运行。 The app is being bootstrapped like this: 该应用程序正在这样启动:

export class AppModule {
    constructor(
        private upgradeModule: UpgradeModule,
    ) {}

    ngDoBootstrap() {
        this.upgradeModule.bootstrap(
            document.body,
            ['myApp'],
            { strictDi: true },
        )
    }
}

However, I've started to rewrite components in Angular and I've run into problems trying to get those downgraded components to work. 但是,我已经开始用Angular重写组件,并且在尝试使这些降级的组件正常工作时遇到了问题。 I'm using ui-router to map components to states like this: 我正在使用ui-router将组件映射到这样的状态:

function config($stateProvider) {
    $stateProvider.state('some-state', {
        parent: 'app',
        views: {
            content: {
                component: 'someComponent'
            }
        },
        url: '/some-state'
    })
}

The rewritten component is being downgraded like this: 重写的组件正在这样降级:

angular
   .module('myApp')
   .directive(
       'someComponent',
       downgradeComponent({ component: SomeComponent }),
   )

SomeComponent is just a regular Angular component. SomeComponent只是常规的Angular组件。

It shows up fine, the template is being rendered and everything, however, change detection doesn't seem to be working. 它显示得很好,正在渲染模板,并且一切正常,但是,更改检测似乎不起作用。 The change detector seems to run once shortly after the view is being rendered, after that however, it doesn't ever run again unless i call it manually. 更改检测器似乎在渲染视图后不久就运行一次,但是在那之后,除非我手动调用它,否则它永远不会再次运行。 NgZone.isInAngularZone() returns false if run anywhere within the downgraded component, I'm not sure if this is intended or not. 如果在降级的组件内的任何位置运行NgZone.isInAngularZone()返回false ,我不确定这是否是有意的。 If I inject ChangeDetectorRef and run detectChanges on any methods that change the fields of the component, the updates are being detected and rendered just fine. 如果我注入ChangeDetectorRef和运行detectChanges在不改变组件领域的任何方法,都被检测并呈现就好了更新。 However, it would be way too much work in the long run to append an (input)="changeDetectorRef.detectChanges()" to every input, and call detectChanges() in pretty much any function that does anything. 但是,从长远来看,将(input)="changeDetectorRef.detectChanges()"附加到每个输入,然后在几乎任何detectChanges()任何事情的函数中调用detectChanges() ,将是太多的工作。

Is there anything I am missing? 我有什么想念的吗? If I haven't provided enough information, I can try to get a minimal reproduction of the problem running, this would probably be a bit of work though, so I want to ask like this first. 如果我没有提供足够的信息,我可以尝试对运行中的问题进行最小化的再现,尽管这可能会花点时间,所以我想先这样问。

Ok, so the answer turned out to be something really simple. 好的,答案很简单。 While I upgraded everything individually, I copied over most of the old index.html , which still contained ng-app="myApp" on the body tag. 当我分别升级所有文件时,我复制了大部分旧的index.html ,该文件仍在body标签上包含ng-app="myApp"

This caused the AngularJS app to bootstrap itself in the HTML instead of the Angular app handling the bootstrapping, which prevented the AngularJS app from running in the NgZone. 这导致AngularJS应用程序在HTML中进行自我引导,而不是在处理引导程序的Angular应用程序中进行自我引导,这阻止了AngularJS应用程序在NgZone中运行。 I'm leaving this here for anyone who makes the same mistake. 我要把这个留给犯同样错误的人。 In short, make sure you've removed the old ng-app -tag from your index.html . 简而言之,请确保已从index.html中删除了旧的ng-app -tag

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM