ajax请求在Angularjs中循环

时间:2017-04-11 10:02:46

标签: javascript angularjs ajax

您好我正在开发Angularjs应用程序。我正在对服务器进行ajax调用以获取一些细节。请求是POST。为Ajax编写的代码是循环的,并没有停止。

(function () {
    angular.module('RoslpApp').controller('ChangePassword', ['$scope', '$http', '$translatePartialLoader', '$translate', '$state', '$stateParams', 'cfg', 'toastr',
        function ($scope, $http, $translatePartialLoader, $translate, $state, $stateParams, cfg, toastr) {
            var url = cfg.Baseurl + "api/Customer/ResetPassword";
            //  alert(SomeFactory.getData());
            var url = cfg.Baseurl + "api/Customer/ModifyPassword";
            var ID = $stateParams.LoginID;
            debugger;
            //this loops and never stops
            $scope.changePassword = function () {
                var resetpassword = {
                    ID: ID,
                    NewPassword: $scope.otpnewpassword
                };
            $http.post(url, resetpassword).then(function (response) {

            }, function (error) {

            });
            }
        }]);
})();

这是我的HTML代码。

<div class="body-wrapper background-pattern">
    <div class="form-wrapper">
        <div class="form-container">
            <h2 class="form-header">Change Password</h2>
            <div class="form-row">
                <div class="inputblock">
                    <span class="input-icon"><img src="images/lock-icon.png"></span>
                    <input type="password" class="with-icon" placeholder="New Password" ng-model="newpassword">
                </div>
                <div class="inputblock">
                    <span class="input-icon"><img src="images/lock-icon.png"></span>
                    <input type="password" class="with-icon" placeholder="Confirm Password">
                </div>
            </div>
            <div class="button-container">
                <input type="button" value="Change Password" id="input-submit" ng-bind="changePassword()">
                <input type="submit" value="Cancel" id="input-cancel">
            </div>
        </div>
    </div>
</div>

我不确定为什么Ajax调用是循环的。我可以在这里得到一些帮助解决这个问题吗?任何帮助,将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:1)

您正在使用ng-bind和函数,ngBind属性告诉AngularJS替换指定HTML元素的文本内容尝试 ng-click 而不是。

<input type="button" value="Change Password" id="input-submit" ng-bind="changePassword()">

  <input type="button" value="Change Password" id="input-submit" ng-click="changePassword()">