javascript - AngularJS中如何将第三方库回调(firebase)的数据传递给View

标签 javascript angularjs firebase angularjs-apply

我正在使用 angularjs 和 firebase 上传文件 我正在尝试从 Controller 传递参数以在 state_changed 事件中查看 它不起作用,我可以在 Controller 上打印它但不能在 View 中使用它

我用过的这些代码

// File or Blob named mountains.jpg
var file = "";

// Create the file metadata
var metadata = {
  contentType: 'image/jpeg'
};

// Upload file and metadata to the object 'images/mountains.jpg'
var uploadTask = storageRef.child('images/' + file.name).put(file, metadata);

// Listen for state changes, errors, and completion of the upload.
uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED, // or 'state_changed'
  function(snapshot) {
    // Get task progress, including the number of bytes uploaded and the total number of bytes to be uploaded
    var progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
    console.log('Upload is ' + progress + '% done');
    $scope.progress = progress;
    switch (snapshot.state) {
      case firebase.storage.TaskState.PAUSED: // or 'paused'
        console.log('Upload is paused');
        break;
      case firebase.storage.TaskState.RUNNING: // or 'running'
        console.log('Upload is running');
        break;
    }
  }, function(error) {
  switch (error.code) {
    case 'storage/unauthorized':
      // User doesn't have permission to access the object
      break;

    case 'storage/canceled':
      // User canceled the upload
      break;

    ...

    case 'storage/unknown':
      // Unknown error occurred, inspect error.serverResponse
      break;
  }
}, function() {
  // Upload completed successfully, now we can get the download URL
  var downloadURL = uploadTask.snapshot.downloadURL;
});
<pre>{{progress}}</pre>

最佳答案

因为firebase事件发生在AngularJS框架之外,$scope的变化需要使用$scope.$apply()

$scope.$apply(function() {
    $scope.progress = progress;
});

来自文档:

Angular modifies the normal JavaScript flow by providing its own event processing loop. This splits the JavaScript into classical and Angular execution context. Only operations which are applied in Angular execution context will benefit from Angular data-binding, exception handling, property watching, etc... You use $apply() to enter Angular execution context from JavaScript.

Keep in mind that in most places (controllers, services) $apply has already been called for you by the directive which is handling the event. An explicit call to $apply is needed only when implementing custom event callbacks, or when working with third-party library callbacks.

有关详细信息,请参阅

关于javascript - AngularJS中如何将第三方库回调(firebase)的数据传递给View,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42508380/

相关文章:

javascript - 如何处理嵌套上下文?

angularjs - 当对象包含 ng-repetate 时,如何使用 angularFire 保存 Firebase 对象 $asArray()

AngularJS Controller 提交表单

javascript - 编译后操作模板

firebase - 如何在 VS Code 中为 Firebase 启用 IntelliSense?

javascript - Rails 双渲染

javascript - D3 zoomable sunburst order child

javascript - Javascript 中的属性值

android - Android Studio 在线数据库

database - 如何在 firestore 中获取 setData 的 Id?