Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.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 组件中更新双向绑定值后的角度调用回调 myApp.component('example'{ 模板:“单击我”, 绑定:{value:'=',回调:'&'}, 控制器:函数(){ this.click=函数(){ this.value='clicked'; 这是callback(); }.约束(本); }, }); myApp.component('useExample'{ 模板:“”, 控制器:函数(){ this.callback=函数(){alert(this.value);}.bind(this); }, });_Javascript_Angularjs - Fatal编程技术网

Javascript 组件中更新双向绑定值后的角度调用回调 myApp.component('example'{ 模板:“单击我”, 绑定:{value:'=',回调:'&'}, 控制器:函数(){ this.click=函数(){ this.value='clicked'; 这是callback(); }.约束(本); }, }); myApp.component('useExample'{ 模板:“”, 控制器:函数(){ this.callback=函数(){alert(this.value);}.bind(this); }, });

Javascript 组件中更新双向绑定值后的角度调用回调 myApp.component('example'{ 模板:“单击我”, 绑定:{value:'=',回调:'&'}, 控制器:函数(){ this.click=函数(){ this.value='clicked'; 这是callback(); }.约束(本); }, }); myApp.component('useExample'{ 模板:“”, 控制器:函数(){ this.callback=函数(){alert(this.value);}.bind(this); }, });,javascript,angularjs,Javascript,Angularjs,这里有两个组件,而第二个组件使用第一个组件 第一个组件更改this.value,然后调用callback。但是当第二个警报(this.value)时,它得到的是空值,而不是第一次“单击”。触发回调时,useExample中的this.value似乎没有更新 我想得到新的价值,而不是旧的价值 我尝试将示例中的this.callback()更改为类似于的$timeout(函数(){this.callback();}.bind(this),0),它可以工作。但我认为应该有更好的办法 因此,我的问题是,

这里有两个组件,而第二个组件使用第一个组件

第一个组件更改
this.value
,然后调用
callback
。但是当第二个
警报(this.value)
时,它得到的是空值,而不是第一次“单击”。触发回调时,
useExample
中的
this.value
似乎没有更新

我想得到新的价值,而不是旧的价值

我尝试将
示例中的
this.callback()
更改为类似于
的$timeout(函数(){this.callback();}.bind(this),0)
,它可以工作。但我认为应该有更好的办法

因此,我的问题是,我应该怎样做才能使
useExample
在回调中读取新的
this.value

--更新1--

我宁愿不更改给定的接口

--更新2--


啊哈,我刚刚找到了这个话题:。看来这个问题和那个问题是重复的。我读过关于这个问题的帖子,似乎
$timeout
是最好的(?)方式,wt*

问题是,在表达式绑定中调用函数之后,将值从子作用域绑定到父作用域的观察器在微线程(光纤)上执行

解决方案是在表达式绑定中将值公开为局部值:

  myApp.component('example', {
    template: '<button type="button" ng-click="$ctrl.click()">click me</button>',
    bindings: { value: '=', callback: '&' },
    controller: function () {
      this.click = function () {
        this.value = 'clicked';
        this.callback();
      }.bind(this);
    },
  });

  myApp.component('useExample', {
    template: '<example value="$ctrl.value" callback="$ctrl.callback()"></example>',
    controller: function () {
      this.callback = function () { alert(this.value); }.bind(this);
    },
  });
由于该值是作为回调参数提供的,因此该值立即可用


问题在于,在表达式绑定中调用函数之后,将值从子作用域绑定到父作用域的观察程序在微线程(光纤)上执行

解决方案是在表达式绑定中将值公开为局部值:

  myApp.component('example', {
    template: '<button type="button" ng-click="$ctrl.click()">click me</button>',
    bindings: { value: '=', callback: '&' },
    controller: function () {
      this.click = function () {
        this.value = 'clicked';
        this.callback();
      }.bind(this);
    },
  });

  myApp.component('useExample', {
    template: '<example value="$ctrl.value" callback="$ctrl.callback()"></example>',
    controller: function () {
      this.callback = function () { alert(this.value); }.bind(this);
    },
  });
由于该值是作为回调参数提供的,因此该值立即可用


.

在回调中传递值不是我想要的。因为它会改变我的组件的接口,使调用程序更加混乱。(调用者不是我写的)我只是希望它能像
那样工作,这对我来说更好,而且看起来更“标准”。在回调中传递值不是我想要的。因为它会改变我的组件的接口,使调用程序更加混乱。(呼叫者不是我写的)我只是希望它能像
那样工作,这对我来说更好看,看起来更“站得住脚”。可能的重复
myApp.component('useExample', {
    template: '<example callback="$ctrl.useCallback($value)"></example>',
    controller: function () {
      this.useCallback = (v) => { alert(v); };
    },
});