【发布时间】:2017-09-05 07:06:54
【问题描述】:
我的问题是我的 AngularJS(最新 v1.X)无法添加到我在 IE 中生成的 DOM svg 元素。
我在开发控制台中没有显示任何错误。我的应用程序已加载(因此也加载了控制器),因为 svg 容器的宽度和高度由 Angular 设置。
该应用程序在 Firefox、Chrome、平板电脑 Chrome 和 Android 浏览器上运行良好...
我的指令:
'use strict';
My_App.directive('ngHtmlCompile', ["$compile", function ($compile) {
return {
restrict: 'A',
replace: true,
link: function (scope, element, attrs) {
scope.$watch(function () {
return scope.$eval(attrs.ngHtmlCompile);
}, function (value) {
element.html(value);
$compile(element.contents())(scope);
});
}
}
}]);
我的 HTML:
<!DOCTYPE html>
<html lang="fr" ng-app="My_App" ng-controller="App">
<head>
<meta charset="UTF-8">
<title>My App</title>
<style>
* { margin:0; padding:0; @import url('https://fonts.googleapis.com/css?family=Open+Sans'); font-family: 'Open Sans', sans-serif; font-weight: bold; } /* Retire les espaces autour du canvas */
html, body { width:100%; height:100%; } /* Override le comportement des navigateurs */
svg { display:block;background-color: dimgrey;} /* Retire la scrollbar */
svg text {
cursor: default;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
::selection, ::-moz-selection{background: none;}
</style>
<script type="text/javascript" src="angular/angular.min.js"></script>
<script type="text/javascript" src="js/MyApp.js"></script>
</head>
<body>
<svg id="MyApp" ng-cloak ng-attr-width="{{svg.width}}" ng-attr-height="{{svg.height}}" ng-html-compile="html"></svg>
</body>
</html>
控制器方面,我的 $scope.html 在启动时使用 XHR 请求更新一次,然后每 5 秒更新一次。然后调用 ngHtmlCompile 来读取它,并将存储的 html 作为 svg 的子项添加。
如果有什么遗漏,你可以问我。第一次发帖,如果我的请求做错了什么,请随时告诉我。最后,对不起,如果我的英语不好,我是法国人。
感谢您的帮助。
【问题讨论】:
-
在 IE 上你不能使用 .html() 方法来创建 SVG 元素。 IE11 发布后,规范更改为支持此功能。因此,较新的浏览器确实支持这一点。
-
感谢您的回复。我很高兴知道现在问题出在哪里。那么有什么解决方案可以在 IE11 上运行吗?我可以使用画布,但这意味着重写每个绘图函数以支持画布。
-
我不能只使用 ng-view,并使用应用程序在 html 文件中构建 svg 元素吗?我应该试试。
标签: angularjs internet-explorer svg microsoft-edge