Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 角度2:作为@Input属性的函数_Javascript_Angular_Angular2 Changedetection - Fatal编程技术网

Javascript 角度2:作为@Input属性的函数

Javascript 角度2:作为@Input属性的函数,javascript,angular,angular2-changedetection,Javascript,Angular,Angular2 Changedetection,你可以在这里看到我的名字。 在这个非常简单的例子中,我传递了一个函数 _ => { console.log(number); } 指向具有@Input属性的子组件。我的父组件如下所示: @Component({ selector: 'my-app', template: ` <child [func]="getFunction(3)"> </child>

你可以在这里看到我的名字。 在这个非常简单的例子中,我传递了一个函数

   _ => {
    console.log(number);
  }
指向具有@Input属性的子组件。我的父组件如下所示:

@Component({
    selector: 'my-app',
    template: `

                <child [func]="getFunction(3)">
                </child>
                <button type="button" (click)="startChangeDetection()">
                  Start change detection
                </button>
    `,
    directives : [Child],
    styles:[`

          .titles {
                    color:#0099FF
             }
            .child-style {
                    background-color:#00ffff
            }
            ` ],
})
export class CarComponent {
    startChangeDetection()
    {

    }
    getFunction(number)
    {
      return _ => {
        console.log(number);
      }
    }
}

为什么Angular甚至叫ngOnChanges?为什么它认为有任何变化呢?

每次调用getFunction时,该方法都会返回不同的函数实例

getFunction(number)
{
  return _ => {
    console.log(number);
  }
}
由于,每次运行更改检测时都会调用getFunction

绑定到函数通常不是最好的主意。如果以这种方式移出函数创建,则每次都会返回相同的函数实例,角度变化检测不会将其识别为变化:

myCallback = _ => this.myCallBack(number) {
  console.log(number);
}

getFunction(number)
{
  return this.myCallback;
}

每次调用getFunction时,此方法返回不同的函数实例

getFunction(number)
{
  return _ => {
    console.log(number);
  }
}
由于,每次运行更改检测时都会调用getFunction

绑定到函数通常不是最好的主意。如果以这种方式移出函数创建,则每次都会返回相同的函数实例,角度变化检测不会将其识别为变化:

myCallback = _ => this.myCallBack(number) {
  console.log(number);
}

getFunction(number)
{
  return this.myCallback;
}

我没有看到任何异常。更改检测将被调用两次

第一次,因为子组件在汽车组件内部渲染。组件树已更改

第二次,因为函数getFunction在作为getFunction3传递给输入时被调用。这相当于输入值的变化,从而触发变化检测周期


我没有看到任何异常。更改检测将被调用两次

第一次,因为子组件在汽车组件内部渲染。组件树已更改

第二次,因为函数getFunction在作为getFunction3传递给输入时被调用。这相当于输入值的变化,从而触发变化检测周期


“为什么Angular甚至调用ngOnChanges?”——这一部分有点简单:DOM事件触发CarComponent上的更改检测,从而导致重新解释模板,再次调用getFunction3,从而导致值更改为不同但相同的函数,从而导致子组件更新。如果子组件有一个OnPush变更检测策略;因为它没有,所以不管输入是否更改,更改检测都会为它运行。为什么要在输入上传递函数??您的概念是错误的,可能您必须以不同的方式为您的子组件增值。让我们知道原因,我们可以为您想出一个解决方案。tl可能重复;dr:改用事件/输出。“Angular为什么还要调用ngOnChanges?”——这一部分有点简单:DOM事件触发CarComponent上的更改检测,从而导致重新解释模板并再次调用getFunction3,这将导致值更改为不同但相同的函数,从而导致子组件更新。如果子组件有一个OnPush变更检测策略;因为它没有,所以不管输入是否更改,更改检测都会为它运行。为什么要在输入上传递函数??您的概念是错误的,可能您必须以不同的方式为您的子组件增值。让我们知道原因,我们可以为您想出一个解决方案。tl可能重复;dr:改用事件/输出。是的!就是这样:是的!就是这样: