我正在尝试使用已知的工作网络方法 (.net) 更新我的 Angular 模型。
我成功从记录到控制台的 Webmethod 获取数据。
它也会更新输入框的值。
但这不会更新我的 Angular 模型绑定(bind)。
我做错了什么?
HTML
<input class="btn-primary" />
<input class="txtCalls" ng-model="TotalCalls"/>
Angular
$scope.TotalCalls;
隐藏代码
[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public static string GetCallData()
{
String Calls = "34";
return Calls;
}
Ajax 调用
$(document).ready(function () {
$('.btn-primary').click(function () {
$.ajax({
type: "GET",
url: "Stats.aspx/GetCallData",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
$('.txtCalls').text(response.d);
console.log(response.d);
},
failure: function (response) {
alert(response.d);
console.log(response.d);
}
});
});
最佳答案
这是因为这是 JQUERY/Ajax 调用而不是 Angular 调用。您需要从 Angular Controller 进行调用并使用 $http.get
而不是 $.ajax
您的按钮 .btn-primary
应添加 ng-click="someMethod()"
而不是 JQuery on('click')
.喜欢:
<input class="btn-primary" ng-click="someMethod() />
我假设您已经有一个 Controller ,因此您需要在其中实现someMethod
,例如:
$scope.someMethod = function(){ $http.get(...) };
此外,Angular 是一个 MVVM 框架,作为此模式的一部分,您可以更新模型(数据)而不是 View html>form>input
控件,两种方式绑定(bind)将为您完成剩下的工作。
关于javascript - 如何在 Angular 中使用 webmethod 单击输入按钮时保持绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23700421/