angular - 通过组件方法将类插入具有属性绑定的正文元素时出现问题
问题描述
我刚开始学习 Angular,这是我在 Stackoverflow 上的第一篇文章,如果我的问题看起来太明显,我很抱歉,但我自己真的找不到答案......
该项目由两个组件组成,一个在另一个内部。在<table>
“外部”有一个,我需要在它的<tr>
标签上添加一个 (click) 事件来触发属性绑定到页面的 body 元素,特别是插入一个保存到 .scss 全局文件中的类,使用属性“溢出:隐藏”。我已经导出了外部的组件并将其导入到 app.module 中,尽管它似乎没有什么不同。
Chrome 读取 html 代码,就好像那里没有任何括号一样......
Chrome 的 F12 ->
<body [class.body-overflow-hidden]=" displayBody == bodyoverflowhidden'" class="theme-origin">
索引.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Time de Vendas</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="./assets/img/favicon.png">
</head>
<body [class.body-overflow-hidden]=" displayBody == 'bodyoverflowhidden'" class="theme-origin">
<app-root></app-root>
</body>
</html>
应用模块
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { ExtratopilotoModule } from './extratopiloto/extratopiloto.module';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { HomeComponent } from './home/home.component';
import { NotFoundComponent } from './error/not-found/not-found.component';
import { FeedComponent } from './feed/feed.component';
import { ProfileComponent } from './profile/profile.component';
import { TopsComponent } from './tops/tops.component';
import { StatementComponent } from './profile/statement/statement.component';
import { ExtratopilotoComponent } from './extratopiloto/extratopiloto.component';
@NgModule({
declarations: [
AppComponent,
HomeComponent,
NotFoundComponent,
FeedComponent,
ProfileComponent,
TopsComponent,
StatementComponent,
],
imports: [
BrowserModule,
AppRoutingModule,
ExtratopilotoModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
外部组件
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-extratopiloto',
templateUrl: './extratopiloto.component.html',
styleUrls: ['./extratopiloto.component.scss']
})
export class ExtratopilotoComponent implements OnInit {
display: string = '';
displayBody: string = '';
displayProposal() {
this.display = "modal-on";
this.displayBody = "bodyoverflowhidden";
}
hideProposal() {
this.display = "";
this.displayBody = "";
}
constructor() { }
ngOnInit() {
}
}
外部模块
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ExtratopilotoComponent } from './extratopiloto.component';
@NgModule({
imports: [
CommonModule
],
declarations: [
ExtratopilotoComponent
],
exports: [ExtratopilotoComponent]
})
export class ExtratopilotoModule { }
编辑:
文件->“外部组件”
如果有人需要确切地知道代码是如何结束的,这里是:
import { Component, OnInit, Renderer2} from '@angular/core';
@Component({
selector: 'app-extratopiloto',
templateUrl: './extratopiloto.component.html',
styleUrls: ['./extratopiloto.component.scss']
})
export class ExtratopilotoComponent implements OnInit {
display: string = '';
displayBody: string = '';
displayProposal() {
this.display = "modal-on";
this.renderer.addClass(document.body, 'body-overflow-hidden');
}
hideProposal() {
this.display = "";
this.renderer.removeClass(document.body, 'body-overflow-hidden');
}
constructor(private renderer: Renderer2) { }
ngOnInit() {
}
}
解决方案
我认为与其试图将这些组件联系在一起,不如将所有逻辑包含在 1 个组件中
尝试使用 Renderer2
将其导入包含您的点击事件的组件中
export class ExtratopilotoComponent implements OnInit {
constructor(private renderer: Renderer2) { }
onChangeBodyClick() {
this.renderer.addClass(document.body, 'bodyoverflowhidden');
}
}
这样一来,您的代码总体上更干净,您只需查看 1 个位置即可进行更改。
推荐阅读
- sql - SQL 以 5 结束价格
- c# - 找不到具有 CLSID {91493441-5A91-11CF-8700-00AA0060263B} 的 Powerpoint COM 对象来设置权限
- excel - 在匹配excel中的特定单元格后尝试引用可变行中的数据
- javafx - 使用插入 toString 的静态嵌套类
- jquery - ($(window).width() > 768) 在 768 像素处不中断
- angularjs - 由于以下原因无法实例化模块 dataAccessService:错误:[$injector:nomod]
- java - 我有一些代码可以打印出给定数组的摘要,但想知道如何打印位置而不是总和中的值?
- html - 如何使用 flex-wrap 让两侧的 div 具有相同的空间?- 弹性盒
- python - 我的代码不想输出提取的数据
- javascript - 更新其中的所有值后如何返回修改后的对象?