javascript - 如何将 $watch 用于 getBoundingClientRect()
问题描述
我有一个 iframe,当源更新为 iframe 时,根据加载时间(有些包含图像,因此需要时间来加载)iframe 的宽度和高度,即;getBoundingClientRect()
变化。所以每次我想触发事件时,如何使用触发器使用$watch
.
解决方案
第一种方法:
您可以使用 $scope.$watch() 观看函数
HTML
<div ng-app="myApp" ng-controller="myCtrl">
<div id="header">Example Header</div>
</div
JS
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
let h = document.getElementById('header');
$scope.$watch(function(){
return h.getBoundingClientRect();
}, function(newValue, oldValue){
console.info("log", newValue, oldValue);
});
});
第二种方法:
MutationObserver:MutationObserver 接口提供了监视对 DOM 树所做更改的能力。( https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver )
快乐编码!!!
推荐阅读
- database - 导出本地数据库和导入服务器数据库时 WordPress 菜单丢失
- php - 在 MySQL PHP 中从多行结果中分离行
- javascript - 在我的 chrome 扩展中将框架加载到我的选项页面中
- postgresql - 重写 pl/pgSQL 函数
- r - 使用样本创建多个训练、验证、测试拆分
- android - 将带有过滤器的 SVG 转换为 Android Vector Drawable
- angular - mat-autocomplete 不使用 observable 过滤
- vue.js - 如何从 url vue 中删除 /#/
- c# - 使用 DataReceivedEventHandler 函数中的字符串更新主窗体的文本框控件?
- flutter - 侦听错误的 Flutter Provider,但仍然收到错误“setState() 或 markNeedsBuild() 在构建期间调用。”