gpt4 book ai didi

javascript - 从事件回调内部更改绑定(bind) Angular 变量

转载 作者:行者123 更新时间:2023-11-30 12:41:22 27 4
gpt4 key购买 nike

我正在尝试在加载视频时更改输入字段的值。相关代码如下:

这个有效:

$scope.stopRecording = function () {
// value of the input changes as expected
$scope.videoEndPos = 10;
};

这不是

$scope.stopRecording = function () {

video.onloadedmetadata = function() {

$scope.videoEndPos = 10;

// the value of the input does not change, but this still correctly outputs 10
console.log($scope.videoEndPos);
};

};

为了保持简短,我在那里遗漏了一些重要的视频内容,但那部分正在工作并且 onloadedmetadata 正在正确触发,所以它的 Angular 和输入有些时髦。但是,如果您怀疑我遗漏了一些相关代码,请告诉我。

最佳答案

video.stopRecording 发生在 Angular 世界之外,因此它不知道变化。你需要使用的是$scope.$apply ,它允许您从 Angular 外部执行对范围的更改。

$scope.stopRecording = function () {

video.onloadedmetadata = function() {
$scope.$apply(function(){
$scope.videoEndPos = 10;
});

// the value of the input does not change, but this still correctly outputs 10
console.log($scope.videoEndPos);
};

};

关于javascript - 从事件回调内部更改绑定(bind) Angular 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24332422/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com