【问题标题】:How to pass changed variable from Child component to Parent component without using @Output?如何在不使用@Output 的情况下将更改的变量从子组件传递到父组件?
【发布时间】:2018-07-11 14:18:04
【问题描述】:

我不知道如何在 angular 4 指令中使用两种方式绑定。

在 AngularJs 中我曾经这样做过:

<random-directive binded='name' binded2="xyz"></random-directive>

并在指令定义中在名称后添加=。现在我可以从指令控制器或link 更改name

但在Angular 2-6 教程中,我无法找到如何做到这一点。 这样做:

<random-directive [binded]='name' [binded2]='name2'></random-directive>

导致只绑定到名称的一种方式。但是我想从指令本身更改变量,这可能吗?

【问题讨论】:

  • 你可以使用 ngModel

标签: javascript angular


【解决方案1】:

This blog helped me a lot:

上面两个答案都没有指定的一件事是要发出的 valueChange 以便自动反映值的变化。这让我感到困惑。

//If using ngModel this is the html
<input [ngModel]="username" (ngModelChange)="username = $event">
// Or go with [(ngModel)]
<p>Hello {{username}}!</p>
//The component here
@Component({

 selector: 'custom-counter',
  template: `
    <button (click)="decrement()">-</button>
    <span>{{counter}}</span>
    <button (click)="increment()">+</button>
  `
})
export class CustomCounterComponent {

  counterValue = 0;

  get counter() {
    return this.counterValue;
  }

  set counter(value) {
    this.counterValue = value;
  }

  decrement() {
    this.counter--;
  }

  increment() {
    this.counter++'
  }
}


// The parent here
@Component()
export class CustomCounterComponent {

  counterValue = 0;

  @Input()
  get counter() {
       return this.counterValue;
     }
      ...
  }
  //The html
<custom-counter [(counter)]="someValue"></custom-counter>

<p>counterValue = {{someValue}}</p>
//The directive with model config
@Component()
export class CustomCounterComponent {

  ...
  @Output() counterChange = new EventEmitter();

  set counter(val) {
    this.counterValue = val;
    this.counterChange.emit(this.counterValue);
  }
  ...
}

【讨论】:

    【解决方案2】:
    <random-directive [(ngModel)]='name'></random-directive>
    

    为了使用ngModel,你必须在你的模块中导入FormsModule from "@angular/forms"

    这是我为您制作的示例 plunker:

    https://plnkr.co/edit/AReq0QngbE9130Bd38Qq?p=preview

    您不能对单个ngModel 使用多个变量,但可以将其绑定到一个对象。如果你在你的 ts 中定义一个像这样的对象:

    public myObject = { name: 'John', surname: 'Doe' }
    

    然后您可以将多个输入绑定到您的对象属性,如下所示:

    <input [(ngModel)]="myObject.name" />
    <input [(ngModel)]="myObject.surname" />
    

    根据您的编辑,您需要使用@Input()

    在您的 .ts 组件中,在组件的开头声明 @Input() binded;@Input() binded2;

    export class RandomDirective {
       @Input() binded;
       @Input() binded2;
    }
    

    那么你可以使用

    <random-directive [(binded)]=“myVar” [(binded2)]=“myVar2”><random-directive>
    

    【讨论】:

    • 你可以在 [(ngModel)] 中使用多个变量吗?
    • 我已经编辑了@Gianluca Paris 的问题,请相应地编辑你的答案,因为我想我无法解释我想要什么,所以我为此创建了一个 plunk。
    • 以及如何使用@Input() 或@Output 请编辑你的plunk 我卡在这里很长时间了
    • 完成,请同时查看文档angular.io/guide/component-interaction
    【解决方案3】:

    双向绑定

    角度 1

     <input ng-model="username">
    

    Angular2及以上版本

     <input [(ngModel)]="username">
    

    所有版本的单向绑定

    <p>Hello {{username}}!</p>
    

    您可以查看documentation of two way binding in angular: 了解更多信息。

    【讨论】:

    • 你可以在 [(ngModel)] 中使用多个变量吗?
    • @BlackMamba 不,据我所知你不能,原因是你可以在绑定时绑定单个变量,但是你也可以使用方法。
    • @PardeepJain 在 Angular Js 中,我们过去只使用 ng-model 进行输入,例如 stackoverflow.com/questions/14908133/… questioni
    • @BlackMamba,是的,我知道。感谢您的链接:)
    • 您能否提供我更新问题中要求的方法
    猜你喜欢
    • 1970-01-01
    • 2019-02-06
    • 2020-10-17
    • 2018-03-16
    • 2018-10-05
    • 2019-02-24
    • 2017-11-01
    • 2017-05-19
    • 1970-01-01
    相关资源
    最近更新 更多