在角度addEventListener
中, NULL
下面是路径名中的代码( src / assets / js / custom.js )
const signUpButton = document.getElementById('signUp');
const signInButton = document.getElementById('signIn');
const container = document.getElementById('container');
signUpButton.addEventListener('click', () => {
container.classList.add('right-panel-active');
});
signInButton.addEventListener('click', () => {
container.classList.remove('right-panel-active');
});
HTML
<!-- main section -->
<div class="container container text-center" id="container">
<div class="form-container sign-up-container">
<!-- Sign Up form code goes here -->
<form action="#">
<h1 class="text-center">Create Account</h1>
<div class="social-container">
<a href="#" class="social"><i class="fab fa-facebook-f"></i></a>
<a href="#" class="social"><i class="fab fa-google-plus-g"></i></a>
</div>
<span>or use your email for registration</span>
<input type="text" class="form-control custInp" placeholder="Name" />
<input type="email" class="form-control custInp" placeholder="Email" />
<input type="password" class="pse" placeholder="Password" />
<button>Sign Up</button>
</form>
</div>
<div class="form-container sign-in-container">
<!-- Sign In form code goes here -->
<form action="#">
<h1>Sign in</h1>
<div class="social-container">
<a href="#" class="social"><i class="fab fa-facebook-f"></i></a>
<a href="#" class="social"><i class="fab fa-google-plus-g"></i></a>
<a href="#" class="social"><i class="fab fa-linkedin-in"></i></a>
</div>
<span>or use your account</span>
<input type="email" class="form-control custInp" placeholder="Email" />
<input type="password" class="form-control custInp" placeholder="Password" />
<a href="#">Forgot your password?</a>
<button>Sign In</button>
</form>
</div>
<div class="overlay-container">
<!-- The overlay code goes here -->
<div class="overlay">
<div class="overlay-panel overlay-left">
<h1>Welcome Back!</h1>
<p>
To keep connected with us please login with your personal info
</p>
<button class="ghost" id="signIn">Sign In</button>
</div>
<div class="overlay-panel overlay-right">
<h1>Hello, Friend!</h1>
<p>Enter your personal details and start journey with us</p>
<button class="ghost" id="signUp">Sign Up</button>
</div>
</div>
</div>
</div>
<!-- main section -->
当我在不使用 的情况下使用此代码时, 角度 的使用效果很好。
但是在角度上我遇到了 错误:
Uncaught TypeError: Cannot read property 'addEventListener' of null
请让我知道我在做什么错以及如何解决?
答案 0 :(得分:1)
使用Angular提供的功能来代替手动操作DOM。让它处理事件,处理类等。在TS中需要做的就是声明一个指示面板是否处于活动状态的属性
isRightPanelActive: boolean;
和模板中
<div class="container text-center" [class.right-panel-active]="isRightPanelActive">
<!-- ... -->
<button class="ghost" id="signUp" (click)="isRightPanelActive = true">Sign Up</button>
<!-- ... -->
<button class="ghost" id="signIn" (click)="isRightPanelActive = false">Sign In</button>
答案 1 :(得分:0)
在这里您进行此更改,它将按您的要求运行 添加ngclass来检查条件并使用功能toggleClass()
test.component.html文件
<div class="container" id="container" [ngClass]="{'right-panel-active':activeClass}">
<div class="form-container sign-up-container">
<form action="#">
<h1>Create Account</h1>
<div class="social-container">
<a href="#" class="social"><i class="fab fa-facebook-f"></i></a>
<a href="#" class="social"><i class="fab fa-google-plus-g"></i></a>
<a href="#" class="social"><i class="fab fa-linkedin-in"></i></a>
</div>
<span>or use your email for registration</span>
<input type="text" placeholder="Name" />
<input type="email" placeholder="Email" />
<input type="password" placeholder="Password" />
<button>Sign Up</button>
</form>
</div>
<div class="form-container sign-in-container">
<form action="#">
<h1>Sign in</h1>
<div class="social-container">
<a href="#" class="social"><i class="fab fa-facebook-f"></i></a>
<a href="#" class="social"><i class="fab fa-google-plus-g"></i></a>
<a href="#" class="social"><i class="fab fa-linkedin-in"></i></a>
</div>
<span>or use your account</span>
<input type="email" placeholder="Email" />
<input type="password" placeholder="Password" />
<a href="#">Forgot your password?</a>
<button>Sign In</button>
</form>
</div>
<div class="overlay-container">
<div class="overlay">
<div class="overlay-panel overlay-left">
<h1>Welcome Back!</h1>
<p>
To keep connected with us please login with your personal info
</p>
<button class="ghost" id="signIn" (click)="toggleClass()" >Sign In</button>
</div>
<div class="overlay-panel overlay-right">
<h1>Hello, Friend!</h1>
<p>Enter your personal details and start journey with us</p>
<button class="ghost" id="signUp" (click)="toggleClass()" >Sign Up</button>
</div>
</div>
</div>
</div>
test.component.ts文件
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-double',
templateUrl: './double.component.html',
styleUrls: ['./double.component.css']
})
export class DoubleComponent implements OnInit {
constructor() { }
activeClass=false;
ngOnInit() {
}
toggleClass(){
this.activeClass=!this.activeClass
}
}