简体   繁体   English

将唯一标识符传递给angular 2组件?

[英]Passing unique identifier to angular 2 component?

I am facing an issue in Angular2. 我在Angular2中遇到了一个问题。 I want to pass unique identifier to my reusable component. 我想将唯一标识符传递给我的可重用组件。 Can someone help me. 有人能帮我吗。

account.html

<div class="container">
      <!-- ACCOUNT INFO -->
      <div class="row">
        <!-- ACCOUNT FORM -->
        <div class="col-sm-12">
          <div class="row">
            <div class="col-sm-4">
              <p>Name</p>
            </div>
            <div class="col-sm-6">
              <ar-account-editable-string [type]="'text'" [name]="'name'" [placeholder]="'Enter the name'"
                [text]="user?.username" (changed)="user.lastName = $event">
              </ar-account-editable-string>
            </div>
            <div class="col-sm-2 action-type">
              <div *ngIf="user.lastName">
                <a href (click)="enableAction()">Edit</a>
              </div>
              <div *ngIf="!user.lastName">
                <a href (click)="enableAction()">Add</a>
              </div>
            </div>
            <div class="col-sm-12">
              <ar-toggleable-account-section [inactive]="inactive" (activated)="$event">
              </ar-toggleable-account-section>
            </div>
          </div>
          <div class="row">
            <div class="col-sm-4">
              <p>Password</p>
            </div>
            <div class="col-sm-6">
              <div *ngIf="oldPassword">
                <ar-account-editable-string [type]="'password'" [name]="'oldPassword'" [placeholder]="'Current Password'" [text]="oldPassword" (changed)="oldPassword = $event">
                </ar-account-editable-string>
              </div>
              <div *ngIf="newPassword">
                <ar-account-editable-string [type]="'password'" [name]="'newPassword'" [placeholder]="'New Password'" (changed)="newPassword = $event">
                </ar-account-editable-string>
                <ar-account-editable-string [type]="'password'" [name]="'repeatNewPassword'" [placeholder]="'Repeat New Password'" (changed)="newPassword = $event">
                </ar-account-editable-string>
              </div>
            </div>
            <div class="col-sm-2 action-type">
              <div *ngIf="user.password">
                <a href (click)="enableAction()">Edit</a>
              </div>
              <div *ngIf="!user.password">
                <a href (click)="enableAction()">Add</a>
              </div>
            </div>
            <div class="col-sm-12">
              <ar-toggleable-account-section [inactive]="inactive" (activated)="$event">
              </ar-toggleable-account-section>
            </div>
          </div>

In above html ar-toggleable-account-section is inactive. 在上面的html中,ar-toggleable-account-section处于非活动状态。 It will become active when user either clicks 'Edit' or 'Add' button. 当用户点击“编辑”或“添加”按钮时,它将变为活动状态。

account.component.ts account.component.ts

protected inactive: boolean = true;
protected enableAction():boolean {
    this.inactive = false;
    return false;
  }

toggleable-account.component.html 可切换-account.component.html

<div class="col-sm-offset-4" *ngIf="!inactive">
  <a href (click)="save()">Save</a>
  <a href (click)="cancel()">Cancel</a>
</div>

toggleable-account.component.ts 可切换-account.component.ts

import {
  Component, Input, Output, ViewChild, ViewEncapsulation, EventEmitter
} from '@angular/core';

@Component({
  selector: 'ar-toggleable-account-section',
  templateUrl: './toggleable-account.component.html',
  encapsulation: ViewEncapsulation.None
})
export class ToggleableAccountButtonComponent {

  /**
   * set the inactive state of the component
   * @type {boolean}
   */
  @Input() inactive:boolean = true;

  @Output() action = new EventEmitter<string>();

  protected save():void {
    this.action.emit('save');
  }

  protected cancel():void {
    this.action.emit('cancel');
  }

}

editable-account-string.component.html: 编辑帐户,string.component.html:

<input type="{{type}}"
        placeholder="{{placeholder}}"
        value="{{text}}" 
        [readonly]="text ? 'true': null" />

editable-account-string.component.ts: 编辑帐户,string.component.ts:

import {
  Component, Input, Output, ViewEncapsulation, EventEmitter
} from '@angular/core';

@Component({
  selector: 'ar-account-editable-string',
  templateUrl: './editable-account-string.component.html',
  encapsulation: ViewEncapsulation.None,
})
export class EditableAccountStringComponent {
  @Input() type:string = 'text';
  @Input() placeholder:string = 'Enter some text...';
  @Input() name:string = '';
  @Input() text:string = '';

  @Output() changed = new EventEmitter<string>();


}

Issue: 问题:

Initially 'ar-toggleable-account-section' is inactive/hide. 最初'ar-toggleable-account-section'处于非活动状态/隐藏状态。 When I click 'Edit' or 'Add' button it becomes active but shows me multiple 'Save' 'Cancel' on the screen because same [inactive] is used in multiple places that are 'lastName', 'password' etc. So, when one [inactive]="inactive" become false all will show on the screen. 当我点击“编辑”或“添加”按钮时,它会变为活动状态,但会在屏幕上显示多个“保存”“取消”,因为在“lastName”,“password”等多个位置使用相同的[inactive]。所以,当一个[inactive] =“inactive”变为false时,屏幕上将显示所有内容。 How can we pass unique/dynamic [inactive] value to the component for each user.lastName, user.password, user.email etc.? 我们如何将每个user.lastName,user.password,user.email等的唯一/动态[非活动]值传递给组件?

Can someone give me a solution for this? 有人可以给我一个解决方案吗?

You could try this, pass what you Adding/Editing to the enable function and then in the components' input test whether that adding/editing property matches what you are adding/editing 您可以尝试这一点,将您添加/编辑的内容传递给启用功能,然后在组件的输入测试中添加/编辑属性是否与您添加/编辑的内容相匹配

account.component.ts account.component.ts

protected addEditProp = '';
protected enableAction(prop: String) {
    this.addEditProp = prop;
}
protected finishedAddEdit(result: String) {
    this.addEditProp = '';
}

toggleable-account-component.ts (just a variable name change) toggleable-account-component.ts (只是变量名称更改)

@Output() accountResult = new EventEmitter<string>();

account.html (Notice the accountResult in the <ar-toggleable-account-section> ) account.html (注意<ar-toggleable-account-section>的accountResult)

<div class="col-sm-2 action-type">
    <div *ngIf="user.lastName">
        <a href (click)="enableAction('lastName')">Edit</a>
    </div>
    <div *ngIf="!user.lastName">
        <a href (click)="enableAction('lastName')">Add</a>
    </div>
</div>
<div class="col-md-12>
    <ar-toggleable-account-section [inactive]="addEditProp === 'lastName'" (accountResult)="finishedAddEdit($event)">
    </ar-toggleable-account-section>
</div>
<div class="col-sm-2 action-type">
    <div *ngIf="user.password">
        <a href (click)="enableAction('password')">Edit</a>
    </div>
    <div *ngIf="!user.password">
        <a href (click)="enableAction('password')">Add</a>
    </div>
</div>
<div class="col-sm-12">
    <ar-toggleable-account-section [inactive]="addEditProp === 'password'" (accountResult)="finishedAddEdit($event)">
    </ar-toggleable-account-section>
</div>

You will have to make sure that when you are done adding/editing and emit save or cancel to the parent component, it looks like the finishedAddEdit(result: String) function, you reset the addEditProp. 您必须确保在完成添加/编辑并向父组件发出保存或取消后,它看起来像finishedAddEdit(result: String)函数,您重置addEditProp。

addEditProp = '';



[UPDATE] (To Show input if user is editing the lastName/password ect.) [更新] (如果用户正在编辑lastName / password等,则显示输入。)

So to show the input field for the lastName or password based on which "Edit" button they clicked. 因此,要根据他们单击的“编辑”按钮显示lastName或密码的输入字段。

I added the *ngIf and if the userAction is edit and the addEditProp is 'lastname' 我添加了*ngIf ,如果userAction是编辑而addEditProp'lastname'

<div class="col-sm-6" *ngIf="userAction === 'edit' && addEditProp === 'lastname'">
     <ar-account-editable-string [type]="'text'" [name]="'name'" [placeholder]="'Enter the name'" [text]="user?.username" (changed)="user.lastName = $event">
     </ar-account-editable-string>
</div>
<div class="col-sm-2 action-type">
    <div *ngIf="user.lastName">
        <a href (click)="enableAction('lastName', 'edit')">Edit</a>
    </div>
    <div *ngIf="!user.lastName">
        <a href (click)="enableAction('lastName', 'add')">Add</a>
    </div>
</div>

account.component.ts account.component.ts

protected addEditProp = '';
protected userAction = '';
protected enableAction(prop: String, userAction: String) {
    this.addEditProp = prop;
    this.userAction = userAction;
}
protected finishedAddEdit(result: String) {
    this.addEditProp = '';
    this.userAction = '';
}

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

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