Angular JS works in Chrome, but not IE 11
Asked Answered
T

4

13

I'm picking up Angular JS at: http://www.sitepoint.com/practical-guide-angularjs-directives/, and I find that the following codes work in Chrome, but not IE 11.

<!DOCTYPE html>
<html ng-app="myapp">
<head>
    <meta charset="utf-8" />
    <title>No Title</title>
    <script data-require="[email protected]" src="http://code.angularjs.org/1.2.7/angular.js" data-semver="1.2.7"></script>
</head>
<body>
    <input type="text" ng-model="color" placeholder="Enter a color..." />
    <div data-hello-world />
    <script>
        var app = angular.module('myapp', []);
        app.directive('helloWorld', function () {
            return {
                restrict: 'AE',
                replace: true,
                template: '<p style="background-color:{{color}}">Hello World!!</p>',
                link: function (scope, elem, attrs) {
                    elem.bind('click', function () {
                        elem.css('background-color', 'white');
                        scope.$apply(function () { scope.color = "white"; });
                    });
                    elem.bind('mouseover', function () { elem.css('cursor', 'pointer'); });
                }
            }
        });
    </script>
</body>
</html>

Specifically, the mouseover and click events work fine. However, the paragraph's background color doesn't in IE (the color never changes). It's ok in Chrome. Thanks!

Thigpen answered 19/5, 2014 at 7:41 Comment(7)
Try ngStyle instead of the classic style attribute.Grania
I've tried '<p ng-style="{background-color:color}">Hello World!!</p>', and it doesn't work in both IE and Chrome. I'm running out of quotes...! Both single and double quotes are already used. :(Thigpen
Escape the quotation marksInflectional
I tried '<p ng-style="{&#39;background-color&#39;:color}">Hello World!!</p>' and it worked in both browsers. Thanks!Thigpen
As an aside note, '<p ng-style=&#34;{&#39;background-color&#39;:color}&#34;>Hello World!!</p>' doesn't work in both browsers. Don't escape too much, don't escape too little. What headache!Thigpen
@Dunnomuch, try to move your script into the head tag of the html, removing the scripts from the html body, I was with this problem in IE11 and FF and it solved my problem. If you still have this problem try this, and let us know.Lactometer
There are several other solutions in this stackoverflow question.Staford
A
8

Could be because document compatibility. This worked for me:

Add this tag to web.config.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <httpProtocol>
      <customHeaders>
        <clear />
        <add name="X-UA-Compatible" value="IE=10" />
      </customHeaders>
    </httpProtocol>
  </system.webServer>
</configuration> 
Antiicer answered 16/10, 2014 at 21:9 Comment(2)
This worked for me! I was just trying to confirm Angular would work because my company has their IE locked down pretty good. So, I had just a very simple one line Angular expression with no controllers or anything like that. Didn't work in IE 11 but would in Chrome. Only difference I changed was that I used "IE-11" instead of "IE-10".Twomey
If you're working on a website and can't take it offline to make this change, but have a .master (if an asp.net website) file then adding <meta http-equiv="X-UA-Compatible" content="IE=11" /> to the master's <head> can be a good solution too. Thanks to @James Gates R below for that suggestion.Pram
T
4

I added the following to the head and it worked. its very similiar to what Mark said...just none asp.net specific:

<meta http-equiv="X-UA-Compatible" content="IE=11" />

i also found i needed to add respond and modernizer in a if statement for older versions of IE:

<!--[if lt IE 9]>
<script src="/Binders/Scripts/modernizr-2.8.3.js"></script>
<script src="/Binders/Scripts/respond.js"></script>
<![endif]-->
Trunkfish answered 16/9, 2016 at 2:52 Comment(1)
This was a lifesaver. Even though my angularjs app had been happily working with IE up until now, and even though I didn't change the angular.min.js file (it was v 1.4.4), this problem popped up overnight after a release to a client. I couldn't take the client offline during business hours, but could make this change on the website's master page to fix it without taking out every else's session.Pram
M
3

Use ng-style tags instead of style="{{CSS}}". The latter works in Chrome and Firefox but does not work in Internet Explorer <= 11.

Maze answered 24/4, 2015 at 3:25 Comment(0)
C
0

Try "use strict" directive before controller

"use strict";
function controller($scope) {
    // your code....
} 
Climber answered 1/3, 2016 at 8:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.