javascript - 如何在 Angular 中使用 webmethod 单击输入按钮时保持绑定(bind)

标签 javascript jquery angularjs webmethod

我正在尝试使用已知的工作网络方法 (.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/

相关文章:

javascript - 如何根据选择选项访问变量的值?

javascript - document.normalize 究竟做了什么?

jquery - 使用 jQuery 制作每个单独行的列表

jQuery文件上传: Is it possible to trigger upload with a submit button?

javascript - onmouseover 和 jquery 函数无法正常工作

node.js - Azure - 每个 Web 应用程序进行多个部署?

javascript - 如何在 Angular 应用程序中隐藏损坏的图像

javascript - 如何仅触发一次 window.resize ?

javascript - 使用 JavaScript 读取 html/body 标签 margin-top

javascript - $http get angularjs 无法传递参数?