小编BBa*_*ger的帖子

ng2-charts:设置颜色不起作用

我的图表定义为:

  <canvas #chart baseChart
              [data]="_chartData"
              [labels]="_chartLabels"
              [chartType]="_chartType"
              [colors]="_backgroundColors">
  </canvas>
Run Code Online (Sandbox Code Playgroud)

我将颜色指定为:

private _backgroundColors:string[] = ["#345657", "#112288", "#adf444"];
Run Code Online (Sandbox Code Playgroud)

一切正常,但颜色。如果我传入这个数组,所有颜色都显示为相同的颜色,浅灰色。任何人都看到可能有什么问题?

typescript ng2-charts angular

5
推荐指数
1
解决办法
3873
查看次数

Angular 2,如何获取全局激活的路由?

如何在不涉及路由的组件中获取ActivatedRoute?

我尝试这样:

constructor(
  private _route: ActivatedRoute,
) {
  this._route.params.subscribe((params: Params) => {

    console.log(params['param1'], params['param2']);

  });

}
Run Code Online (Sandbox Code Playgroud)

它记录未定义,未定义。

typescript angular2-routing angular

5
推荐指数
1
解决办法
5068
查看次数

前端应用程序的安全性

我对一般用户身份验证(前端和后端)的任何事情都很陌生。我正在使用 Angular 2/5 构建一个应用程序。我找到了Jason Watmore 的这篇教程,我将尝试根据我的需要进行调整。我在评论中看到,杰森解释说,对于希望前端安全的网站,建议在单独的前端应用程序中进行身份验证,然后将用户重定向到完整的应用程序,一旦用户原因是没有前端应用程序是安全的,因为用户可以更改变量来查看受路由保护或其他方式保护的路由。这些数据在后端仍然是安全的,但您在前端具有您不希望公众知道的功能,例如您如何分析用户输入的数据。

我的问题是,就保护前端应用程序而言,这是不可能的绝对事实吗?这是有道理的,我有一种预感这是真的,但我想知道是否有办法至少让查看守卫路线变得非常困难。(就像路由守卫询问服务器令牌是否有效?...但是您可以修改函数以始终返回 true,对吧?嗯...)

[编辑] 延迟加载可以在这里发挥作用吗?是否可以构建一个只有用户登录后服务器才可用的模块/路由?

javascript security authentication frontend angular

5
推荐指数
1
解决办法
2407
查看次数

如何在tsconfig.json中排除以'.spec.ts'结尾的文件

我试图在我的tsconfig.json中包含一些文件,但它包含我不想包含的文件.(我看到在搜索语法使用,随处可见的星号,但我不知道这句法是什么,在哪里可以更多地了解它.)从回购(有未编译源)我想包括结束的文件.ts,除了那些结束的.spec.ts.

以下包括我想要的文件,但没有成功排除我不想要的文件.

  "include": ["node_modules/dashboard/**/*.ts"],
  "exclude": ["node_modules/dashboard/**/*.spec.ts"],
Run Code Online (Sandbox Code Playgroud)

如何排除这些文件?

编辑:起初是使用**/**.ts**/**.spec.ts.这是不正确的,但现在我已经纠正了,我遇到了同样的问题.

javascript json typescript angular

5
推荐指数
1
解决办法
7823
查看次数

多次调用 Angular 2+ ActivationEnd 事件回调

我看到一个订阅router.events产生三个调用,每个路由更改为回调函数,每个调用都报告instanceof eventActivationEnd.

console.log('This code runs only once, at site initialization.');
router.events.subscribe((event: RouterEvent) => {
    if (event instanceof ActivationEnd) {
        console.log('This should only log once per route change, but logs three times.');
    };
});
Run Code Online (Sandbox Code Playgroud)

我在 Github 上找到了这个线程,它似乎相关,但我很难相信这仍然是一个问题......

我正在使用 Angular 5 (5.2.10)。

更新:似乎每个路线段都会触发此事件...正在检查文档。

angular angular-router

5
推荐指数
1
解决办法
4238
查看次数

图像渲染:清晰的边缘和平滑的值在 Chrome 中报告无效的属性值

为什么我会看到这个?根据每种资源,crisp-edges并且smooth应该是该属性的有效值......image-rendering

我都用过:

img {
    image-rendering: smooth;
}

img {
   image-rendering: crisp-edges;
}
Run Code Online (Sandbox Code Playgroud)

我得到:

在此输入图像描述

使用 Chrome 版本 92.0.4515.131。在 Firefox 中正常工作。

是的,两个连字符(名称和值)都出现在正确的位置。

这似乎是 Chrome 中的一个错误,但我想确定这不是我造成的。

css image

4
推荐指数
1
解决办法
2178
查看次数

使用 Angular 2+ [innerHTML] 添加包含样式属性的 html

我正在使用 Angular 2+ [innerHTML] 输入来插入 HTML 格式,包括样式标签。

在我的模板中,我有类似的东西:

<span [innerHTML]="someVar"></span>
Run Code Online (Sandbox Code Playgroud)

在我的组件中,我有:

someVar = `<span style="background-color:#990000">test</span>`;
Run Code Online (Sandbox Code Playgroud)

我收到警告:

WARNING: sanitizing HTML stripped some content (see http://g.co/ng/security#xss).
Run Code Online (Sandbox Code Playgroud)

在输出中,插入的跨度完好无损,减去样式属性。

所以我使用了这篇文章中的管道:

/sf/ask/2595380721/

看起来像:

import { Pipe, PipeTransform } from '@angular/core';
import { DomSanitizer, SafeHtml } from '@angular/platform-browser';

@Pipe({name: 'safeHtml'})
export class SanitizeHtml implements PipeTransform {
  constructor(private sanitizer: DomSanitizer) {}

  transform(html): SafeHtml {
    // return this.sanitizer.bypassSecurityTrustStyle(style);
    return this.sanitizer.bypassSecurityTrustHtml(html);
    // return this.sanitizer.bypassSecurityTrustScript(value);
    // return this.sanitizer.bypassSecurityTrustUrl(value);
    // return this.sanitizer.bypassSecurityTrustResourceUrl(value);
  }
}
Run Code Online (Sandbox Code Playgroud)

这与以前没有区别,尽管我不确定我是否以正确的方式使用它......

如何使用innerHTML让Angular保留我的样式属性?

javascript css angular

3
推荐指数
1
解决办法
6284
查看次数

使npm启动脚本在特定端口运行服务

我有一些脚本package.json,我需要知道如何让start脚本正确接受-portangular-cli 的参数.

  "scripts": {
    "ng": "ng",
    "start": "ng serve  --proxy-config proxy.conf.json",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
Run Code Online (Sandbox Code Playgroud)

我们需要这个,因为我们想同时运行我们软件的多个实例.目前,如果我有一个项目在默认端口4200上运行,并尝试npm start -port 4300在第二个终端窗口中运行,我得到,"端口4200已经在使用中.使用'--port'指定不同的端口."

我该怎么做才能让我的构建在特定端口运行?我怎么能这样做,以便我可以从命令行将端口号传递到npm启动脚本?

node.js npm angular

3
推荐指数
2
解决办法
6121
查看次数

将表单状态对象中的“禁用”传递给FormControl构造函数不起作用

我们之前已经看过了:

看来您正在使用带有反应形式指令的Disabled属性。如果在组件类中设置此控件时将Disabled设置为true,则实际上将在DOM中为您设置disabled属性。我们建议使用这种方法来避免“检查后更改”错误。

带有警告输出的示例:

  form = new FormGroup({
    first: new FormControl({value: 'Nancy', disabled: true}, Validators.required),
    last: new FormControl('Drew', Validators.required)
  });
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试像它说的那样做时,它是行不通的。因此,我在SO上搜索了这个问题,然后发现了许多类似这样的问题,但是他们都说要FormControl.disable()手动致电。我不想这样做,但是将disabled“表单状态对象”传递给构造函数不起作用。为什么不?

这是我的代码:

this.registerForm('someName', {
  firstName: new FormControl({disabled: true}),
});
Run Code Online (Sandbox Code Playgroud)

这不是一个重复这个问题,因为我是问为什么有些东西不能工作是应该,而无需额外的函数调用,或添加东西的模板。通过答案我发现的结果很关键,并且可能是对Angular设计的疏忽。

angular

3
推荐指数
1
解决办法
2780
查看次数

从 Angular 2+ 中的自定义指令中读取/获取元素属性

我正在构建一个自定义指令,我想在其中读取本机元素的属性 (formControlName) 之一,然后有条件地向该元素添加一个或多个属性。

但是,当我 console.log 原生元素的属性时,我得到:

不明确的

这是我尝试过的:

@Directive({
  selector: '[appInputMod]'
})
export class InputModDirective implements OnInit {

  constructor(public renderer: Renderer2, public hostElement: ElementRef) { }

  @Input()
  appInputMod() { }

  ngOnInit() {

    console.log(this.hostElement.nativeElement.formcontrolname);

    const el = this.hostElement.nativeElement;
    if (el.formcontrolname === 'firstName')
    {
        this.renderer.setAttribute(this.hostElement.nativeElement, 'maxlength', '35');
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

如何从指令中读取此属性名称?

angular2-directives angular

3
推荐指数
1
解决办法
6529
查看次数