形式是不是在$就成功清除函数值,采用了棱角分明 [英] Form is not clearing values in success function of $.ajax, using angular

查看:114
本文介绍了形式是不是在$就成功清除函数值,采用了棱角分明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个形式,我使用angualr.js。我绑定的输入值的角度变量。提交形式,在$阿贾克斯成功功能后,我分配空值,这些变量,但我在形式上,而不是空字符串看到相同的值。
下面是我的形式code,

I have a form in that, I am using angualr.js. I am binding input values to angular variables. After submitting form, in success function of $.ajax, I am assigning empty values to these variables, but I am seeing same values in form instead of empty strings. Below is my form code,

 <form name="addForm"  ng-submit="outClear()">
      <table>
       <tr>
          <td> <label>Title</label></td>
          <td> <input type="text" id="idTitle" ng-model="Title"/> </td>
        </tr>
        <tr>
           <td> <label>Description</label></td>
           <td> <textarea id="idDescription" name="desc" ng-model="Description" ></textarea> </td>
        </tr>
        <td><input type="submit" class="btn btn-default" value="Add" />  </td>
         <td></td>
         </tr>
        </table>
</form>

下面是我的Java脚本code,

Below is my java script code,

var MyApp = angular.module("MyApp", []);
  MyApp.controller("TicketCntrl", function ($scope) {
 $scope.outClear = function () {
    var Title = $scope.Title;
    var Description = $scope.Description;

    var Ticket = {
        "Title": String(Title),
        "Description": String(Description),
    };

    $.ajax({
        type: "POST",
        url: "/Home/Add",
        data: Ticket,
        success: function (data, status) {
        alert('In success');
            $scope.clear();
        },
        error: function (status, error) {
        }
    });


};
$scope.clear = function () {
    $scope.Title = '';
    $scope.Description = '';
    alert('In clear');
  }
 }

这将是成功的功能,并显示在成功的消息,并要明确()和分配''值$ scope.Title和$ scope.Description但在文本框和文本区域,我看到相同的价值观其中我已经进入。

It is going to success function and showing 'In Success' message and going to clear() and assigning '' values to $scope.Title and $scope.Description but in text box and text area I am seeing same values which I have entered.

一件事我试过了,而不是outClear(),我直接提交后调用clear()。然后,它是完美的工作(所有表单值越来越空的)。

one more thing I tried, instead of outClear(), I directly call clear() after submitting. Then it is perfectly working(all form values getting empty).

请让我知道我做错了。

推荐答案

不要使用jQuery混合角操纵范围的变量,它会搞砸与消化角周期。如果从jQuery的做更新范围变量它不会运行周期消化

Don't mix angular with jQuery for manipulating scope variables, it will messed up with digest cycle of angular. It will not run digest cycle if you do update scope variable from jQuery.

您需要运行通过调用 $适用() $范围你的方法后,<$ C消化周期$ C>清()在阿贾克斯成功修复它很快发出,但最好我不会preFER去了这种方法。

You need to run digest cycle by calling $apply() over $scope after your method clear() in Ajax success to fix it issue quickly, but ideally I wouldn't prefer go for this approach.

而是确实使用 $ HTTP 而不是 $。阿贾克斯

这篇关于形式是不是在$就成功清除函数值,采用了棱角分明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆