我正在使用AngularJS 1.2.26和JQuery 1.11.2。我需要IE8支持,所以不使用AngularJS中的$ http.get,而是使用JQuery的$ .get并将为IE8支持添加jQuery ajaxTransport扩展:
{{ testText }}
...
var app = angular.module("myApp", ['ui.bootstrap']);
app.controller("appController", function($scope) {
$scope.testText = "loading";
$.get("http://www.apicallgoeshere.com").done(function(res) {
$scope.testText = "loaded";
});
});
与$ http.get不同,上面的代码不会将testText更改为“已加载”。如何在$ .get?
中正确更新模型答案 0 :(得分:0)
正如使用$scope.$apply()建议的评论将解决问题。
为什么会发生这种情况,angularjs通过watch更新UI组件,watch会在附加到范围的值发生更改时注册回调方法。但只有在角度方法(digest cycle)内发生变化时才会发生。在你的情况下,你正在使用jQuery ajax并且正在更改范围变量但angularjs没有意识到它,因此UI没有更新。
因此我们使用$apply()方法强制运行与范围相关联的监视。
但您可以使用angularjs提供的$http服务
var app = angular.module("myApp", ['ui.bootstrap']);
app.controller("appController", ['$scope', '$http', function ($scope, $http) {
$scope.testText = "loading";
$http.get("http://www.apicallgoeshere.com").success(function (res) {
$scope.testText = "loaded";
});
});