1

I'm trying to change an input field's value when a video loads. Here's the relevant code:

This works:

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

This doesn't

$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);
    };

};

In an effort to keep this short I left out some crucial video stuff there, but that part is working and the onloadedmetadata is firing properly, so its something funky with angular and the input. However, let me know if you suspect I've left out some relevant code.

2 Answers 2

2

The video.stopRecording happens outside of the Angular universe, so it does not know about the change. What you need to use is $scope.$apply, which allows you to execute changes to the scope from outside of 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);
    };

};
Sign up to request clarification or add additional context in comments.

Comments

1

The video.onloadedmetadata is probably an asynchronous call that doesn't return until after the digest loop. Change it to this:

$scope.stopRecording = function () {

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.