为什么当我尝试在 scope.$watch 中运行时,getElementsByTagName 为空
why getElementsByTagName is null when I try to run in scope.$watch
我是 angularJs 的新手。当"localid"被更改时,我正在尝试在链接内重新运行 init() 函数,但它说
var elm = angular.element(element[0].getSVGDocument().getElementsByTagName("path"));
即使它在页面加载时第一次工作,也为空。
类型错误: 无法读取 null 的属性"getElementsByTagName"
我在谷歌上搜索了 5 个多小时,但我找不到原因。我错过了什么?
(function() {
'use strict';
angular
.module('neuroSeoulFinance')
.directive('seoulmap', seoulMap);
/** @ngInject */
function seoulMap($log) {
var directive = {
restrict: 'E',
template: '<object id="seoulmap" class="localMap" data="../assets/images/seoul.svg" type="image/svg+xml" width="80%"></object>',
replace: true,
scope: {
localid : "="
},
link: function(scope, element, attrs) {
var init = function() {
var elm = angular.element(element[0].getSVGDocument().getElementsByTagName("path"));
$log.log(elm);
for(var i=0; i<elm.length; i++) {
if(elm[i].id == scope.localid.id) {
elm[i].style.fill = attrs.highlight;
}
else {
elm[i].style.fill = "#e1e3e4";
}
}
};
scope.$watch('localid', function(newValue, oldValue) {
if (newValue){
init(); // <-- it doesn't work.
// var ealm = angular.element(element[0].getSVGDocument().getElementsByTagName("path"));
// ^ it also return null..
$log.log("I see a data change!");
}
}, true);
if(element[0].getSVGDocument()) {
$log.log("init");
init();
} else {
$log.log("load");
element.on("load",init);
}
}
};
return directive;
}})();
请参阅如何检查嵌入的 SVG 文档是否已加载到 html 页面中?
如果getSVGDocument返回null,则表示该元素尚未加载,因此如链接中所述,请使用超时,并且由于您使用角度,请使用$timeout。
我在 element.on("load") 完成后更改了$watch代码。感谢您的有用评论。
if(element[0].getSVGDocument()) {
init();
} else {
element.on("load",function(){
scope.$watch('localid', function(newValue, oldValue) {
if (newValue){
init();
}
});
});
}
相关文章:
- 使用压缩的JavaScript文件(不是运行时压缩)
- 如何在运行时在angular 2中加载外部js脚本
- JavaScript错误:Microsoft JScript运行时错误:应为对象
- Google 脚本:用于创建日历活动的脚本运行时不会出错,但不会执行任何操作
- http.listen()在运行时接受终端命令
- 自定义运行时Can'在谷歌应用引擎中看不到我的自定义日志
- 实现比较方法的最佳实践是什么;s的比较类型是在运行时选择的
- JavaScript运行时是如何工作的
- 在运行时创建元素时移到一边时出错
- 如何在运行时在HTML5画布中绘制正方形
- 如何在运行时使用javascript隐藏图像
- 在运行时使用jquery准备表体会导致设计问题
- 使用主题运行时portlet liferay时出现Javascript错误
- JavaScript运行时事件循环现有技术
- Appcelerator Titanium:在运行时下载并解释JavaScript代码
- angularjs ng点击运行时标记不起作用
- 在函数运行时显示对话
- 如何在javascript中在运行时获取对象/数组
- 变量在运行时未定义,但在使用调试器时定义
- 为什么当我尝试在 scope.$watch 中运行时,getElementsByTagName 为空