首页 > 解决方案 > 如何为绑定制作原始类型引用

问题描述

以前,我知道这是一个愚蠢的问题,但我有兴趣找到答案(不要投反对票:))

我曾经在使用 angularJS 时遇到过这个问题,现在我正在学习 angular 6,并希望有一个解决方案(我还没有找到)

我创建了 2 个自定义组件。父组件获取一个用于绑定的对象并将其属性提供给子组件。

例如。

子组件

<div>{{ value }}</div>

父组件

<div>
   <son-component [value]="config.configSonValue"></son-component>
</div>

父亲的用法

<father-component [config]="myConfig"></father-component>

我跳过代码,因为它与问题无关。

这就是myConfig价值

this.sonValue = 1;
const myConfig = {
   configSonValue: this.sonValue // This line is the problem - loosing the reference
}

这使应用程序运行并显示1. 但是,如果我更改 my 中的值this.sonValue不会影响 UI。

这是有道理的,因为当我设置值configSonValue的值this.sonValue时,它的原始类型正在被复制而不是被引用。

今天我要求用户向我提供范围对象和他想要绑定的变量的名称scope[name]......以及我正在做的绑定。

我希望有类似的东西

const myConfig = {
   configSonValue: angular.copyRef(this.sonValue) // wishful solution 
}

这是一个示例https://stackblitz.com/edit/angular-2ta9ug

在那儿 ?

标签: angular

解决方案


推荐阅读