【问题标题】:Angularjs: Able to log data but cannot displayAngularjs:能够记录数据但无法显示
【发布时间】:2017-05-25 02:46:19
【问题描述】:

我正在尝试创建一个表单,在单击提交时调用一个函数,该函数将发布到 php 页面(运行查询),然后在页面上显示这些结果。

如果我在加载时在我的控制器中调用所述函数,我会得到我的预期结果(数据以模态形式显示在 html 表中)。但是,如果我在单击提交时调用该函数。我可以记录数据结果,但它没有显示在我的页面上。

  $scope.report = {};
  var url = "";
  // calling our submit function.
  $scope.submitForm = function() {

      $http.post('url.php').success(function(data) {
          // Stored the returned data into scope 
          $scope.names = data;
          console.log(data);
          $('#myModal').modal();
      });
  };
		
<button type = "button" class="btn btn-success" ng-click="submitForm()" >Submit Request</button>
<div class="modal fade" id="myModal" role="dialog" >
   <div class="modal-dialog">
      <!-- Modal content-->
      <div class="modal-content" >
         <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">&times;</button>
            <h4 class="modal-title">Modal Header</h4>
         </div>
         <div class="modal-body">
            <table class="table table-striped table-bordered">
               <tr>
                  <th>Name</th>
               </tr>
               <tr ng-repeat="name in names | filter:search_query">
                  <td><span>{{name.first}}</span></td>
               </tr>
            </table>
         </div>
         <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
         </div>
      </div>
   </div>
</div>

【问题讨论】:

  • 首先在 html 视图页面上打印你 {{names}} 并查看结果的结构。发布结果是可能的。
  • 我无法将数据结果 {{names}} 打印到页面。我得到的结果就像我试图在模式中显示一样。但是,如果我调用函数 onLoad 而不是 onSubmit,两者都可以工作

标签: javascript php html angularjs


【解决方案1】:

尝试在打开您的模式之前添加一个小的$timeout(不要忘记将$timeout 注入您的控制器):

$scope.submitForm = function() {

      $http.post('url.php').success(function(data) {
          // Stored the returned data into scope 
          $scope.names = data;
          console.log(data);

          $timeout(function () {
              $('#myModal').modal();
          },250);
      });
  };

【讨论】:

  • 我仍然得到原始结果。我还尝试在表格中而不是在模式中显示页面上的数据。console.log(data) 可以,但没有结果显示在页面上
  • @dallas 你能给出记录数据的格式吗?您是否检查过$scope.names 是否具有所需的格式?
  • 如果我 console.log(data) 我得到一个对象数组,格式为 - 0 : Object First : "John"
  • 它是否具有您试图通过{{name.first}} 访问的属性first
猜你喜欢
  • 2021-05-22
  • 1970-01-01
  • 1970-01-01
  • 2016-11-09
  • 1970-01-01
  • 2021-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多