简体   繁体   English

双向绑定 Angular 组件属性到 AngularJS 视图

[英]Two-way binding Angular component property to AngularJS view

So we have an application written in AngularJS, slowly migrating to Angular component-wise.所以我们有一个用 AngularJS 编写的应用程序,慢慢迁移到 Angular 组件。

I'm now building a simple reusable component that is basically a dropdown box with a label.我现在正在构建一个简单的可重用组件,它基本上是一个带有 label 的下拉框。 This is of course in Angular - so I downgrade it into AngularJS, then use it in an AngularJS view.这当然是在 Angular - 所以我将它降级为 AngularJS,然后在 AngularJS 视图中使用它。

How can I now two-way bind a property from the component (let's call it MyDropdown.CurrentValue ) to a scope variable of the AngularJS view (let's call it $scope.numberOfCarsOwned )?我现在如何将组件中的属性(我们称之为MyDropdown.CurrentValue )双向绑定到 AngularJS 视图的 scope 变量(我们称之为$scope.numberOfCarsOwned )?

The component of course has both @Input() CurrentValue: any;该组件当然同时具有@Input() CurrentValue: any; and @Output() CurrentValueChange: EventEmitter<any> , bound to the inner dropdown box.@Output() CurrentValueChange: EventEmitter<any> ,绑定到内部下拉框。

In Angular, I'd just go <MyDropdown [(CurrentValue)]="numberOfCarsOwned" /> , but how do I do this when integrating the component (downgraded as a directive) into my AngularJS view?在 Angular 中,我只需要 go <MyDropdown [(CurrentValue)]="numberOfCarsOwned" /> ,但是在将组件(降级为指令)集成到我的 Z510F541ZCF756EB757 视图中时该怎么做?

So it turns out, once you know how to do it it's totally easy:事实证明,一旦你知道怎么做,就很容易了:

In the AngularJS view, you use it as if you were in Angular:在 AngularJS 视图中,您可以像在 Angular 中一样使用它:

<MyComponent
  [(CurrentValue)]="numberOfCarsOwned"
></MyComponent>

This will automagically bind the CurrentValue (using the component's CurrentValueChange ) to the view's $scope.numberOfCarsOwned .这将自动将CurrentValue (使用组件的CurrentValueChange )绑定到视图的$scope.numberOfCarsOwned

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

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