Angular: Property 'controls' does not exist on type 'AbstractControl'. ng build --prod  error

Running the application using ng serve works fine.

using ng build works fine.

when using ng build --prod --aot or ng build --prod I get the error.

 

出错位置:

<div *ngFor="let address of myForm.get('addresses').controls; let i=index" 

 解决方法:

 

一种方法:

    在ts文件中添加方法:

formData { 
    return <FormArray>this.form.get('Data'); 
}

在html中:

<div *ngFor="let address of formData; let i=index" 

<FormArray>中包括control

第二种方法:

 

直接改变html:

<div *ngFor="let address of myForm.get('addresses')['controls']; let i=index" 

即可!

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: 看起来你的代码中使用了一个名为 `go` 的属性,但是在 `Location` 类型中并没有定义这个属性。请确保你使用的属性在相应的类型中是存在的,或者使用了正确的类型。 举个例子,如果你在某个组件的代码中使用了 `Location` 类型,并希望使用它的 `path()` 方法来获取当前 URL 的路径,你应该这么写: ``` import { Location } from '@angular/common'; ... constructor(private location: Location) {} ... let path = this.location.path(); ``` 如果你误用了 `go` 属性,就会看到类似于 "Property go does not exist on type Location" 的错误信息。 ### 回答2: 在Angular中,当遇到错误消息"Property 'go' does not exist on type 'Location'"时,通常是由于调用了Location类中不存在的方法或属性所导致的。 Location是Angular中用来操作浏览器地址和导航的类。它提供了一系列方法来获取、设置和改变浏览器当前的URL。但是,它并不提供"go"这个方法。 如果我们希望在Angular中进行导航操作,可以使用Router类而不是Location类。Router类是Angular的路由器,它提供了包括导航跳转在内的各种导航功能。 要解决这个错误,我们只需将代码中的"Location.go()"替换为"Router.navigate()"。Router类中的navigate()方法可以接受一个URL或路由路径作为参数,并根据参数实现导航。 举个例子,如果我们原先的代码是这样的: ``` import { Location } from '@angular/common'; // ... export class MyComponent { constructor(private location: Location) {} goBack() { this.location.go('/previous-page'); } } ``` 我们只需将其中的`this.location.go('/previous-page')`替换为: ``` import { Router } from '@angular/router'; // ... export class MyComponent { constructor(private router: Router) {} goBack() { this.router.navigate(['/previous-page']); } } ``` 通过使用Router类的navigate()方法,我们可以解决这个错误,并成功实现在Angular中的导航操作。 ### 回答3: 错误"Property 'go' does not exist on type 'Location'"是因为在Angular中,Location类不具有名为"go"的属性。 Location是Angular中的一个内置服务,用于管理浏览器的URL地址。它提供了一些方法来查看和操作当前URL。然而,Location并不包含"go"方法。 如果您想在应用程序中导航到另一个URL,可以使用Router服务的navigate方法。Router是Angular中用于管理应用程序路由的服务,它提供了一些方法来实现导航。 要使用Router服务进行导航,您需要先在组件构造函数中注入Router服务,然后可以使用其navigate方法来导航到另一个URL。例如,假设您正在创建一个名为"HomeComponent"的组件,并希望在用户点击某个按钮时导航到"/about"页面,您可以按照以下步骤进行操作: 1. 首先,在组件的构造函数中注入Router服务。 ```typescript constructor(private router: Router) { } ``` 2. 然后,在组件的某个方法中使用navigate方法进行导航。 ```typescript onNavigate() { this.router.navigate(['/about']); } ``` 在这个例子中,当用户点击某个按钮时,onNavigate方法将被调用,并通过调用Router服务的navigate方法来导航到"/about"页面。 总而言之,如果您遇到错误"Property 'go' does not exist on type 'Location'",请检查您的代码中是否错误地在Location上使用了"go"属性。如果您想进行页面导航,应该使用Router服务的navigate方法来实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值