IE 11 + AngularJS。 locationChangeStart和preventDefault()上的错误

时间:2014-04-24 16:00:55

标签: angularjs internet-explorer-11

我尝试将改变路线上的提示写入控制器。

代码:

<a href="#/">Test</a>
<a href="#/Test2">Test2</a>
<hr/>

<div ng-app='app'>
    <pre>View:
        <ng-view></ng-view>
    </pre>

</div>

JS:

console.clear();

var app = angular.module("app",["ngRoute"]);

app.controller("Test1", function($rootScope){
    var $offFunction = $rootScope.$on("$locationChangeStart", function(e) {
      if (confirm("Are you sure")) return $offFunction();
      return e.preventDefault();
    });

})

app.config(function($routeProvider){
    var tmpl1 = "Show message when you try change route. Bug: \n1. Click to 'Test2'. On prompt click cancel."
    +"\n2. Click to Test2 again"
    +"\n3. There is no prompt message"
    +"\n <i>Only on IE</i>";
    $routeProvider
    .when("/", {template:tmpl1, controller: "Test1"})
    .when("/Test2", {template:"Test 2"})
})

问题:

在IE11,IE10中,如果单击Test2,请选中“取消”并再次单击“测试2” - 不再调用locationChangeStart。如何解决?

IE9一切正常

JSFiddle

1 个答案:

答案 0 :(得分:1)

问题是由于错放了a标记造成的。它们应该放在ng-app中,因为a不仅仅是tag,而是angular指令。请参阅更正的fiddle