小编amo*_*uda的帖子

Angular:ControlValueAccessor vs @Input - 什么时候用表单?

ControlValueAccessors 在过去几个月突然出现在我面前,我对为什么或何时应该使用它们而不是@Input与我的反应式表单一起使用感到有些困惑。

这是迄今为止我如何使用反应式表单的示例代码:

@Component({
 selector: 'app-my-component',
 template: `<input [formControl]="form.get('specificControlName')"  />` 
 // or the alternative method of [formGroup]="form" and formControlName="specificControlName"
})
export class MyComponent {
 @Input() form: FormGroup; // passed in formGroup

 ngOnInit() {
  form.valueChanges.pipe(
   // do rxjs magic here
  ).subscribe(value => {
   // do something with the value
  })
 }
}
Run Code Online (Sandbox Code Playgroud)

响应式表单保存表单的状态,因此我甚至可以从父组件访问该状态。我还可以访问所有不同的NgControl属性,如validdisableddirty,和touched

ControlValueAccessors 提供了什么,而这种使用反应式表单的方式没有提供?什么是一些使用情况下,ControlValueAccessors优于工作@Input@Output一般的?

编辑

https://medium.com/angular-in-depth/angular-nested-reactive-forms-using-cvas-b394ba2e5d0d

在本文中,作者提到了以下主要区别:

实现嵌套表单的三种方式:

...

  1. 通过 Input 将 FormGroup …

angular

8
推荐指数
1
解决办法
1911
查看次数

如何在 VS Code 中设置 Kotlin 调试 launch.json 配置?

我想在从 Java 迁移到 Kotlin 后调试我的 Kotlin Spring boot 应用程序,但是 Kotlin 的 launch.json 具有与 Java 不同的配置,例如我们用于在profiles环境之间切换,当我添加时,"args": "-Dspring.profiles.active=dev"我得到Property args is not allowed. 在调试控制台中我得到

Internal error: java.lang.IllegalArgumentException:    io.netty:netty-transport-native-epoll:jar:linux-x86_64:4.1.42.Final:compile is not a properly formed Maven/Gradle artifact
[ERROR] java.util.concurrent.CompletionException: java.lang.IllegalArgumentException:    io.netty:netty-transport-native-epoll:jar:linux-x86_64:4.1.42.Final:compile is not a properly formed Maven/Gradle artifact
Run Code Online (Sandbox Code Playgroud)

这是我当前的 Kotlin 配置:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "kotlin",
            "request": "launch",
            "name": "Kotlin Launch",
            "projectRoot": "${workspaceFolder}",
            "mainClass": "path.to.my.Application",
            "args": "-Dspring.profiles.active=dev" // <--- showing error
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

为了进行比较,这是我的 java …

kotlin visual-studio-code

5
推荐指数
0
解决办法
2233
查看次数

Angular 9 生产错误:无法设置只有 getter 的(抽象)类 MyFilter { } 的属性 ?fac

我在库中有一个抽象组件(没有ivy),带有一个@Directive()装饰器,该装饰器具有一些基本属性和函数可以继承给其子级。当我尝试在任何项目中使用该库时,我在浏览器控制台中收到以下错误:

\n\n

Uncaught TypeError: Cannot set property \xc9\xb5fac of class MyFilter {} which has only a getter

\n\n

这是课程:

\n\n
@Directive()\nexport abstract class MyFilter<T> {\n\n  @Input() form: FormGroup;\n  @Input() filterOpened: boolean;\n  @Input() enableSubmit: boolean;\n\n  abstract useFilter();\n}\n\n
Run Code Online (Sandbox Code Playgroud)\n\n

儿童班:

\n\n
@Component({\n  selector: \'my-text-filter\',\n  templateUrl: \'./text-filter.html\',\n  styleUrls: [\'./text-filter.scss\']\n})\nexport class MyTextFilter extends MyFilter<TextFilter> implements OnInit, OnChanges {\n\n  constructor() {\n    super();\n  }\n\n  ngOnInit() {\n  }\n\n  ngOnChanges(changes: SimpleChanges) {\n  }\n\n  useFilter() {\n  }\n\n}\n
Run Code Online (Sandbox Code Playgroud)\n

angular

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

如何访问 stenciljs 中的原生 HTML 属性?以及如何让它出现在文档中?

使用 stenciljs 制作的 Web 组件不扩展 HTMLElement。我如何访问本机属性并在我的组件中使用它们,比如title属性?当我@Prop() title在模板中添加并使用它时,会显示错误。

添加@Prop()还会使属性在生成的文档中可见。如何将使用的或必需的本机属性添加到生成的文档中?

我得到的编译器错误:

The @Prop() name "title" is a reserved public name. Please rename the "title" prop so it does not conflict
with an existing standardized prototype member. Reusing prop names that are already defined on the element's
prototype may cause unexpected runtime errors or user-interface issues on various browsers, so it's best to
avoid them entirely.
Run Code Online (Sandbox Code Playgroud)

javascript web-component typescript stenciljs

2
推荐指数
1
解决办法
825
查看次数

Angular 9:抽象组件的子组件不继承其属性

我有一个包含具有相似属性的组件的模块,因此我创建了一个抽象MyFilter<T>组件,该组件具有一组带有@Injectable装饰器的属性。升级到 Angular 9 后,我在控制台收到警告

无法绑定到“form”,因为它不是“my-text-filter”的已知属性。

和错误,因为子组件无法识别父组件的属性。这是一些代码:

@Injectable()
export abstract class MyFilter<T> {

  @Input() form: FormGroup;
  @Input() filterOpened: boolean;
  @Input() enableSubmit: boolean;
  @Output() filterUsed = new EventEmitter<T>();

  abstract useFilter();
}
Run Code Online (Sandbox Code Playgroud)

这是一个子组件:

@Component({
  selector: 'my-text-filter',
  templateUrl: './text-filter.html',
  styleUrls: ['./text-filter.scss']
})
export class MyTextFilter extends MyFilter<TextFilter> implements OnInit, OnChanges {

  constructor() {
    super();
  }

  ngOnInit() {
    this.form.get('value').valueChanges.pipe( // ERROR: cannot find 'get' of undefined
        debounceTime(50)
    ).subscribe(() => this.useFilter());
  }

  ngOnChanges(changes: SimpleChanges) {
    console.log(changes) // this doesn't get logged in …
Run Code Online (Sandbox Code Playgroud)

angular

2
推荐指数
1
解决办法
1396
查看次数