ajax之angularjs $scope.$apply() 不会更新 ajax IE9 上的选择列表

lvdongjie 阅读:47 2025-06-02 22:19:02 评论:0

所以为了简单起见,我尝试使用从 ajax 调用中获得的新项目列表来更新我的选择列表。列表中有项目。我将模型设置为新列表并执行 $scope.$apply()。这在 Firefox 中效果很好,但在 IE 中无效。我究竟做错了什么?是否有一些我缺少的 IE9 东西? (我现在已经找了几个小时,准备放弃了)。感谢我能得到的所有帮助。

HTML:

<select  
  ng-model="SelectedParameter"  
  ng-options="Parameter.Name for Parameter in AllParameters"> 
</select> 

JS:

$.getJSON("/Cont/GetList", {id: id},  
  function (result) { 
    var allParameters = result.Data.AllParameters; 
    $scope.AllParameters = allParameters; 
    $scope.$apply();   
  } 
); 

请您参考如下方法:

您最好以“Angular 方式”执行此操作。不需要 JQuery!事实上,如果您发现自己以“JQuery 方式”做事,那么您可能做错了。 Mark Rajcok had a really good question (and answer) about this same thing on StackOverflow a while ago :

app.js

//declare your application module. 
var app = angular.module('myApp', []); 
 
//declare a controller 
app.controller('MainCtrl', function($scope, $http) { 
   //function to update the list 
   $scope.updateList = function () { 
      $http.get('/Cont/GetList', function(data) { 
         $scope.allParameters = data; 
      }); 
   }; 
 
   //initial load 
   $scope.updateList(); 
}); 

index.html

<!DOCTYPE html> 
<html ng-app="myApp"> 
<head> 
   <script src="angular.js"></script> 
   <script src="app.js"></script> 
</head> 
<body> 
  <div ng-controller="MainCtrl"> 
    <button ng-click="updateList()">Update</button> 
    <ul> 
       <li ng-repeat="parameter in allParameters">{{parameter | json}}</li> 
    </ul> 
 
    <!-- EDIT: Because you requested a select. 
         or if you wanted to do a select list  
         assuming every object in your array had a "name" property 
         you wanted to display in the option text, you could do 
         something like the following:  
 
         (NOTE: ng-model is required for ng-options to work) 
         --> 
    <select ng-model="selectedValue" ng-options="p as p.name for p in allParameters"></select> 
 
    <!-- this is just to display the value you've selected --> 
    <p>Selected:</p> 
    <pre>{{selectedValue | json}}</pre> 
  </div> 
</body> 
</html> 

编辑:IE 中的一个常见问题

首先,如果您在 IE 中遇到问题,我建议您按 F12 并查看您在控制台中遇到的错误。

我在 IE 中看到的最常见的问题与诸如 console.log() 之类的命令有关,这些命令在 IE 中不存在。如果是这种情况,您需要创建一个 stub ,如下所示:

//stub in console.log for IE. 
console = console || {}; 
console.log = console.log || function () {}; 


标签:ajax
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号