简体   繁体   English

通过 jquery 更新后 Angular 不会重新渲染

[英]Angular doesn't re-render after updating via jquery

I have following html that shows when boolean variable "signUpSuccess" becomes true.我关注 html 显示 boolean 变量“signUpSuccess”何时变为真。

signup.component.html: signup.component.html:

<div class="alert alert-success" *ngIf='signUpSuccess'>User has been registered!</div>

<form>
  <input class="input" type="text" id="username" name="username" [(ngModel)]="username" placeholder="Username"
            required="required" />
...
...

signup.component.ts:注册组件.ts:

import { Component, Inject, OnInit, PLATFORM_ID, ChangeDetectorRef } from '@angular/core';
import { AuthenticationService } from '@shared/services/rest/authentication.service';
import { isPlatformBrowser } from '@angular/common';
import * as $ from 'jquery';

@Component({
  selector: 'app-signup',
  templateUrl: './signup.component.html',
  styleUrls: ['./signup.component.scss']
})
export class SignupComponent implements OnInit {
  public signUpSuccess: boolean = false;
  ...
  ...

  constructor(
    private AuthenticationService: AuthenticationService,
    @Inject(PLATFORM_ID) private platformId: any,
    private cdRef:ChangeDetectorRef // tried, but calling detectChanges() doesn't work
  ) { }

  ngOnInit() {
    if (isPlatformBrowser(this.platformId)) {
      const component = this;
      $(document).ready(function () {
      ...
      ...
      component.handleSignUp();
      ...
      ...

In handleSignUp() that is called via jquery (component.handleSignUp() in ngOnInit function above), it connects to backend and processes signup:在通过 jquery(上面的 ngOnInit function 中的 component.handleSignUp())调用的 handleSignUp() 中,它连接到后端并处理注册:

handleSignUp() {
    this.AuthenticationService.signUp(this.username, this.email, this.name, this.password)
      .subscribe(
        data => {
          this.signUpSuccess = true;
...
...

Upon successful, I give signUpSuccess variable true.成功后,我将 signUpSuccess 变量设为 true。 So "User has been registered."所以“用户已注册”。 should appear automatically.应该会自动出现。

But it doesn't appear automatically.但它不会自动出现。 It appears if and only if I re-write something in that username input field.当且仅当我在该用户名输入字段中重写某些内容时,它才会出现。

How do I automatically shows <div class="alert alert-success" *ngIf='signUpSuccess'>User has been registered!</div> upon successful signup?如何在成功注册后自动显示<div class="alert alert-success" *ngIf='signUpSuccess'>User has been registered!</div>

The issue you are running into is precisely why you are not supposed to mix jQuery and Angular.您遇到的问题正是为什么您不应该混合 jQuery 和 Angular。

The first thing you need to do is remove jQuery from angular.您需要做的第一件事是从 angular 中删除 jQuery。

  1. npm uninstall jquery --save
  2. Remove all jQuery references ( import * as $ from 'jquery'; )删除所有 jQuery 引用( import * as $ from 'jquery';
  3. Remove $(document).ready function删除$(document).ready function
  4. Remove ChangeDetectorRef import.删除ChangeDetectorRef导入。 (You should almost never use this) (你几乎不应该使用它)

You should then be left with a standard component.然后,您应该留下一个标准组件。 Since you were using $.ready , I'm assuming you're wanting your handleSignUp function to be called on component init.由于您使用的是$.ready ,我假设您希望在组件初始化时调用您的handleSignUp function 。 That is precisely what ngOnInit is for (See the NgOnInit Lifecycle Hook for further explanation)这正是ngOnInit的用途(请参阅 NgOnInit Lifecycle Hook 以获得进一步的解释)

You can then correct your code so that your handleSignUp is called on init:然后,您可以更正您的代码,以便在 init 上调用您的handleSignUp

import { Component, Inject, OnInit, PLATFORM_ID } from '@angular/core';
import { AuthenticationService } from '@shared/services/rest/authentication.service';
import { isPlatformBrowser } from '@angular/common';

@Component({
  selector: 'app-signup',
  templateUrl: './signup.component.html',
  styleUrls: ['./signup.component.scss']
})
export class SignupComponent implements OnInit {
  public signUpSuccess: boolean = false;
  ...
  ...

  constructor(
    private AuthenticationService: AuthenticationService,
    @Inject(PLATFORM_ID) private platformId: any,
  ) { }

  ngOnInit() {
    if (isPlatformBrowser(this.platformId)) {
      this.handleSignUp();
    }
  }

It is a bad idea to use jQuery together with Angular.将 jQuery 与 Angular 一起使用是个坏主意。 You need to know Angular change detection mechanism really deep to make jQuery work with Angular.您需要深入了解 Angular 更改检测机制才能使 jQuery 与 Angular 一起工作。 And when you know the Angular change detection mechanism that well, you understand that there is no use of jQuery when you use Angular.而当你对 Angular 变化检测机制了解得那么透彻时,你就会明白,当你使用 Angular 时,没有使用 jQuery。

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

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