【问题标题】:Assigning value to ng-init from Ajax result从 Ajax 结果为 ng-init 赋值
【发布时间】:2016-10-23 12:37:49
【问题描述】:

我有一个带有一个输入字段的表单,用于通过 AJAX 提交 student id,我正在从 AJAX 请求中获取一些 JSON 结果。现在需要将 AJAX 请求返回的数据分配给表单下方输入字段(电话)中的ng-init。请帮助我如何将此值分配给ng-init。谢谢!

HTML

<div ng-app="">
    <form id="std_form">
        <input type="text" name="sid" id="sid">
        <input type="submit">
    </form>

    <input type="text" name="phone" ng-model="phone" ng-init="" id="phone">
    Ph: {{phone}}
</div>

jQuery

$('#std_form').submit(function(e){
    e.preventDefault();
    var val = $('#sid').val();
    var dataString = "sid=" + val;
    $.ajax({
        type: "POST",
        url: "post_process.php",
        data: dataString,
        dataType: 'json',
        success: function(data){
            $('#phone').val(data.phone);
        }
    });
});

post_process.php

<?php

if(ISSET($_POST['sid'])) {

    $sid = $con->real_escape_string($_POST['sid']);

    $sql = "SELECT phone FROM std_detials WHERE sid = $sid";
    $result = $con->query($sql);
    if ($result->num_rows > 0) {
        $row = $result->fetch_assoc();
        $phone = $row['phone'];
    }
    $json = array('phone' => $phone);
    $json = json_encode($json);
    echo $json;
}

?>

【问题讨论】:

  • 需要修改访问php的代码并获取值
  • @Sajeetharan 请告诉我应该对我的代码进行哪些更改?
  • 这个提交全错了,根本不应该涉及jQuery。 ng-init 的想法也是错误的。建议大家学习一些angular form教程,了解ng-model和ng-submit
  • @charlietfl 我知道这是错的,你能纠正我的错误吗?
  • 试图解释这种方法是完全错误的。见thinking-in-angularjs-if-i-have-a-jquery-background。尽管我个人更喜欢ng-submit,但下面提供的解决方案更符合角度方法。 Angular 还内置了您可以利用的验证

标签: php jquery angularjs ajax angularjs-ng-init


【解决方案1】:

您没有将 ng-model 用于学生 ID 字段。当在角度做角说。 该代码混合了传统的 PHP 提交技术,然后通过 Jquery 进行 ajax 调用。尝试尽可能地消除此类做法并正确地崇拜该框架。

试试这个代码。了解并应用。 简介:
我使用了 Angular 的 $http post call 而不是 jquery 的 $.ajax。您可以保持相同的调用或使用本机 Angular 的调用。

删除了一些不必要的标记。在学生 ID 输入中添加了 ng-model,在用于提交的按钮中添加了 ng-click 功能,并对代码进行了一些结构化。如果您有一个单独的 services.js 文件用于项目添加服务代码,或者只是添加一个新服务,就像我在此处的代码中所做的那样。

所以基本上你不需要 ng-init 来解决这个问题。它应该在极少数情况下使用。请在此处阅读文档。 https://docs.angularjs.org/api/ng/directive/ngInit

希望对您有所帮助!如有任何问题,请告诉我。

<div ng-app="myApp">
  <form id="std_form">
    <input type="text" ng-model="sid">
    <button ng-click="submitSid()"></button>
  </form>

  <input type="text" ng-model="phone">
  Ph: {{phone}}
</div>

JS

var app = angular.module('myApp', []);
app.controller('MyCtrl', ['StudentService', function(StudentService){
   $scope.submitSid = function(){
    var data = {sid: $scope.sid};
    StudentService.getStudentDetails(data).then(function(res){
        console.log(res);
        $scope.phone = res.phone; // Or change to res.paramName. Just check in console to confirm the res logged.
    })
}
}]);

 app.factory('StudentService', ['$http', '$httpParamSerializerJQLike',           function($http, $httpParamSerializerJQLike){

  var getStudentDetails = function(data){
  var params = $httpParamSerializerJQLike(data);
    return $http({
        method: "POST",
        url: "post_process.php",
        headers: {'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;'},
        data: params
    }).then(function(response){
      return response.data;
    });
}

return {
    getStudentDetails : getStudentDetails
}   
}]);

【讨论】:

  • 没有任何解释的仅代码答案在 SO 上不是很有价值,无论它们是否解决了问题
  • 只是在编辑相同的内容。匆忙按下提交按钮。道歉
  • 这里还有一些问题......没有为帖子创建键/值对并且需要序列化数据并且success不是$http配置的一部分
  • @charlietfl。感谢您指出。差点错过了序列化的重要部分。更正了两个
  • 好的...但是现在正在序列化什么?需要成为对象而不是原始对象,否则将不会发送键/值对……现在更近了。也应该是return response.data.phone;,因为您已转换为then()
猜你喜欢
  • 1970-01-01
  • 2019-11-15
  • 1970-01-01
  • 2017-03-20
  • 1970-01-01
  • 1970-01-01
  • 2015-12-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多