我正在使用 AngularJS 创建一个 Windows 应用商店应用程序(或 Metro 应用程序,或无论他们如何称呼它)。
我解决了 Javascript 运行时错误“无法添加动态内容”,该错误导致应用程序崩溃(请参阅here http://onehungrymind.com/windows-8-and-angularjs/)一切都很顺利,直到我开始使用指令(理解 angular.module.directive)。
现在,我在控制台日志中看到“无法添加动态内容”。请注意,应用程序不会崩溃,事实上,应用程序按预期工作!
我应该忽略这个错误(我不喜欢这样),我可以做些什么吗?
一个“时钟”应用程序的代码来说明:
该应用程序确实显示了正确的时间,格式化并每秒递增。 DOM 就是我所期望的。
Thanks,
索引.html:
<!doctype html>
<html lang="en" ng-app="phonecat">
<head>
<meta charset="utf-8">
<title>Google Phone Gallery</title>
<link rel="stylesheet" href="css/app.css">
<link rel="stylesheet" href="css/bootstrap.css">
<script src="lib/jquery-1.8.2-win8-1.0.min.js"></script>
<script type="text/javascript">
jQuery.isUnsafe = true;
</script>
<script src="lib/angular/angular.js"></script>
<script src="js/app.js"></script>
<script src="js/controllers.js"></script>
<script src="js/filters.js"></script>
<script src="js/services.js"></script>
<script src="lib/angular/angular-resource.js"></script>
</head>
app.js
angular.module('phonecat', ['phonecatFilters', 'phonecatServices']).config(['$routeProvider', function($routeProvider) {
$routeProvider.
when('/phones', {templateUrl: 'partials/phone-list.html',controller: PhoneListCtrl}).
otherwise({redirectTo: '/phones'});
}])
.directive('myCurrentTime', function($timeout, dateFilter) {
return {
restrict: 'E',
replace: true,
template: '<div> Current time is: <span>{{time}}</span></div>',
link: function (scope, element, attrs) {
var timeoutId;
function updateTime() {
scope.time = dateFilter(new Date(), 'M/d/yy h:mm:ss a');
}
function updateLater() {
timeoutId = $timeout(function () {
updateTime();
updateLater();
}, 1000);
}
element.bind('$destroy', function () {
$timeout.cancel(timeoutId);
});
updateLater();
}
}
});
error:
HTML1701: Unable to add dynamic content '<my-current-time></my-current-time>
'. A script attempted to inject dynamic content or elements previously modified dynamically that might be unsafe. For example, using the innerHTML property to add script or malformed HTML will generate this exception. Use the toStaticHTML method to filter dynamic content or explicitly create elements and attributes with a method such as createElement. For more information, see http://go.microsoft.com/fwlink/?LinkID=247104.
File: index.html