【问题标题】:Bind dropdownlist from jquery ajax call success function using angularjs使用angularjs从jquery ajax调用成功函数绑定下拉列表
【发布时间】:2015-12-12 21:40:10
【问题描述】:

jquery ajax 从服务器返回 json 并尝试使用 angularjs 将返回的 json 绑定到下拉列表中。

我的 ajax 调用:

$.ajax({
type: "POST",
url: "gets",
datatype: "json",
async: true,
data: 
{
'spj': JSON.stringify(spjson),                
'csrfmiddlewaretoken': $("input[name=csrfmiddlewaretoken]").val()
},
success: function (json)
{
var jsonList = json;
}
});

html 中的下拉列表:

<html>
<select ng-model='selvalue' ng-options='val.category for val in vals'> 
</select>
</html>

角度控制器:

<script>
var app = angular.module('ddtest', ['ui.utils']);
app.controller('MainCtrl', function ($scope) 
{
$scope.vals = jsonList 
}
</script>

请建议如何使用 angularjs 从 jquery ajax 调用中绑定下拉列表。

【问题讨论】:

  • 为什么不用$http服务?那是有角度的方式
  • 是的,它正在通过 $http 工作,但我的要求是使用 jquery ajax 服务。
  • 我看不到使用的 $http 服务...为什么有这个要求?

标签: angularjs ajax


【解决方案1】:

首先,为什么不使用角度$http

如果您真的想这样做,您的 ajax 调用将需要存在于您的控制器中(这本身不是一个好习惯),并且在您的调用中......

success: function (json) {
  $scope.vals = json;
  $scope.$apply(); //you will need to apply because angular does not do digest update here
}

但正如我所提到的,理想情况下,您应该创建一个 service 来保存您的 $http 调用或 $resource。

【讨论】:

  • 亲爱的 NenadP,它运行良好。我想要的原因是,我想用 1000 个 uniqe 技能测试性能。非常感谢。
  • 啊,那好吧 - 很高兴为您提供帮助 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-08
  • 2013-01-13
  • 2013-02-14
  • 2016-09-01
  • 2013-01-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多