weixin_33691817 2015-07-29 21:14 采纳率: 0%
浏览 21

温泉重复和Ajax

I've a big problem in my app and I think it's caused by the asynchronous call.

The html part is:

<ons-list ng-repeat="item in newsEventi.items">
   <ons-list-header class="news_titolo_lista">{{item.categoria}}</ons-list-header>
       <ons-list-item class="news_titolo">
        ({{item.titolo}})
       </ons-list-item>           
</ons-list>

The js part (the controller) is:

app.controller('NewsEventiController', ["$scope", function($scope)
{
  //--------------------//
  // private variables
  //--------------------//
  var _self = this;

  var _items = [];

  //--------------------//
  // private functions


  //--------------------//
  // constructor
  (function()
  {

    var url = "http://www.gaelico.net/app/test_json.php";  
    // IT RETURNS -> [{"titolo":"test","categoria":"cat1"},{"titolo":"test1","categoria":"cat2"}]
    $.post(url, {data: JSON.stringify({regID: localStorage.getItem("regID")})},
      function(json)
      {
        _items = JSON.parse(json);
        $scope.$apply();
        console.log("After: " + _items);        
      });    
  })();

  //--------------------//
  // public properties
  //--------------------//

  this.items = _items;
  console.log("Before: " + this.items);

  //************************************************************************//
  // public methods
  //************************************************************************//

}]);

My problem is that the list never display the 2 elements. I used $scope.$apply() because I thought it was the right method to update the list, but it seems not to work.

Thank you in advance for every suggestion.


EDIT I tried to change the script as suggested, and now the code is

app.controller('NewsEventiController', ["$scope","$http", function($scope,$http)
{
  //--------------------//
  // private variables
  //--------------------//
  var _self = this;

  $scope._items = [];

  //--------------------//
  // constructor
  (function()
  {
      // the url returns: [{"titolo":"test","categoria":"cat1"},{"titolo":"test1","categoria":"cat2"}]
      $http.get("https://www.gaelico.net/app/test_json.php")
        .success(function(response) {
          console.log(response);  
          // it shows in the console, correctly: "Object, object"
          $scope._items = response;      
        });

  })();

  //--------------------//
  // public properties
  //--------------------//

   console.log("Before: " + $scope._items);  // empty


}]);

(and in ng-repeat, I put ng-repeat="item in newsEventi._items") But I still don't see any update in the list.

  • 写回答

1条回答 默认 最新

  • csdnceshi62 2015-07-29 21:17
    关注

    Declare items like this:

    $scope._items = []
    

    Your ngRepeat:

    item in _items
    

    You should not need to call apply and i recommend you look into angulars http module instead of using jquery.

    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效