How do you get Angular.js to work in a Windows 8 store app?

末鹿安然 提交于 2019-11-30 11:36:14

The reason is because Angular puts html comment <!-- --> tags for ng-repeat and similar directives. Unfortunately Microsoft considers these to be unsafe when put in from Javascript using .innertHTML and is not allowed. This can be found here:

http://msdn.microsoft.com/en-us/library/windows/apps/hh465388.aspx

It happens at line 1534 in jQLite:

var div = document.createElement('div');
            // Read about the NoScope elements here:
            // http://msdn.microsoft.com/en-us/library/ms533897(VS.85).aspx
            div.innerHTML = '<div>&#160;</div>' + element; // IE insanity to make NoScope elements work!
            div.removeChild(div.firstChild); // remove the superfluous div

What happens is the element is

<!-- ngRepeat: item in arrayTest -->

Then when angular does div.innerHTML = ... the element gets removed. If you put a break point at div.removeChild, you will notice the div.innerHTML is without the element.

[EDIT] After writing this response I tried adding

MSApp.execUnsafeLocalFunction(function () { div.innerHTML = '<div>&#160;</div>' + element });

It works! Apparently you have to wrap all of angularjs in MSApp.execUnsafeLocalFunction and then that line specifically.

I got it working by adding ng-csp to the HTML tag in my store app. This enables Content Security Policy, which disables things like 'eval'. I used the jQuery(v2.1.1) and the Angular(v1.3.0-beta.13).

More on ng-csp in the AngularJS documentation.

in my case I wrapped angularjs with MSApp.execUnsafeLocalFunction and I only used data-ng-repeat instead of ng-repeat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!