简体   繁体   English

如何绑定 Angular4 组件的输入参数

[英]How to bind an input parameter of an Angular4 component

I have a small web site I am developing in Angular4 (my first attempt in Angular) and have come across an issue I just can seem to figure out.我有一个正在 Angular4 中开发的小型网站(我在 Angular 中的第一次尝试)并且遇到了一个我似乎可以解决的问题。 Simplified down my scenario is:简化我的场景是:

I have a component (account-list) which using a html5 select/option control which is populated from a rest api and displays a list of accounts.我有一个组件(帐户列表),它使用一个 html5 选择/选项控件,该控件从一个 rest api 填充并显示一个帐户列表。

I have a second component which displays the details of the account (account-detail) and take accountId as an input parameter.我有第二个组件,它显示帐户的详细信息(account-detail)并将 accountId 作为输入参数。

When an account is selected in the account-list component I want the account-detail to auto update to the newly selected account.在帐户列表组件中选择帐户时,我希望帐户详细信息自动更新到新选择的帐户。 I know that my account-list component is working fine and that when I select an account the selectAccountId variable in the ts is being updated.我知道我的帐户列表组件工作正常,并且当我选择一个帐户时,ts 中的 selectAccountId 变量正在更新。

However I just can't seem to get the update to the selectAccountId variable to trigger an update to the account-detail component.但是,我似乎无法获得对 selectAccountId 变量的更新以触发对帐户详细信息组件的更新。 I know this component works fine on it own as a default account is displayed and the id of this default is set in the ngOnInit method of the account-list component.我知道这个组件可以正常工作,因为显示了默认帐户,并且这个默认值的 id 在 account-list 组件的 ngOnInit 方法中设置。

The relevant html5 in the account-list component: account-list 组件中的相关 html5:

<select id="Id" #Id="ngModel" class="hideLabel form-control" [(ngModel)]="selectedAccountId" name="Id">
    <option [ngValue]="account.Id" *ngFor="let account of accounts">  
        {{account.Name}}  
    </option>  
</select>  

<!-- this div just to prove that selectedAccountId changes when a new account is selected, which it does -->
<div *ngIf="selectedAccountId">
    {{selectedAccountId}}
</div>

<!-- this is the line which does not seem to work, changes to selectedAccountId are not triggering the component to refresh -->
<account-detail *ngIf="selectedAccountId" [account]="selectedAccountId"></account-detail>

The ts code of the account-list component: account-list组件的ts代码:

export class AccountListComponent implements OnInit {
    selectedAccountId: number;

    title: string; 
    accounts: AccountHeader[];
    errorMessage: string;

    constructor(private accountService: AccountService, private router: Router) { }

    ngOnInit() {
        var s = this.accountService.getLatest();

        s.subscribe(
            accounts => {
            this.accounts = accounts; this.selectedAccountId = this.accounts[0].Id;
            },
            error => this.errorMessage = <any>error
        ); 
    }
}

The ts code of the account-detail component: account-detail组件的ts代码:

export class AccountDetailComponent {
    @Input("account") selectedAccountId: number;
    account: Account;

    selectedLicense: License;

    constructor(
        private authService: AuthService, 
        private accountService: AccountService,
        private router: Router,
        private activatedRoute: ActivatedRoute) {
    }

    ngOnInit() {
        if (this.selectedAccountId) {
            this.accountService.get(this.selectedAccountId).subscribe(
                account => this.account = account
            );
    }

        else {
            this.router.navigate([""]);
        }
    }
} 

In all honesty I've lost track of the things I've tried to make this work, most of the blogs, guides etc I've read talk about how to bind the other way and I have all that working just fine.老实说,我已经忘记了我试图使这项工作发生的事情,我读过的大多数博客、指南等都在谈论如何以另一种方式进行绑定,而我所做的一切都很好。 But I can't find how to get the binding to trigger an update of the account-detail component which should be accomplished by this line:但是我找不到如何获取绑定以触发应该由这一行完成的 account-detail 组件的更新:

<!-- this is the line which does not seem to work, changes to selectedAccountId are not triggering the component to refresh -->
<account-detail *ngIf="selectedAccountId" [account]="selectedAccountId"></account-detail>

I've been following a book in which this was working fine, but originally this was working in AngularJS and then migrated to Angular2 (then 4) and somewhere along the way this has stopped working.我一直在关注一本运行良好的书,但最初它在 AngularJS 中运行,然后迁移到 Angular2(然后是 4),并且在此过程中的某个地方停止了工作。

Any help would be much appreciated, thanks!任何帮助将不胜感激,谢谢!

So in your account-detail component you are using the ngOnInit method to fetch some data depending on your selected account id.因此,在您的帐户详细信息组件中,您将使用 ngOnInit 方法根据您选择的帐户 ID 获取一些数据。 ngOnInit is a lifecycle hook that is called once when a component is created. ngOnInit 是一个生命周期钩子,在创建组件时调用一次。 When you change the selected id, angular is not recreating the component and the method does not fire.当您更改选定的 id 时,angular 不会重新创建组件并且不会触发该方法。

What you need is a method that fire when you change the variable.您需要的是一种在更改变量时触发的方法。 There are several approaches you can use, check out this comprehensive post for more information.您可以使用多种方法,查看这篇综合性文章以获取更多信息。

Here is a simple example using a property setter to trigger a method call when the input is changed:这是一个使用属性设置器在输入更改时触发方法调用的简单示例:

export class AccountDetailComponent {
    selectedAccount: Account;

    private _selectedAccountId: number;
    @Input("account") set selectedAccountId(value: number) {
       this._selectedAccountId = value;
       this.getAccount();
    }

    getAccount() {
        if (this._selectedAccountId) {
            this.accountService.get(this._selectedAccountId).subscribe(
                account => this.selectedAccount = account
            );
        } 
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM