dqch34769 2016-04-15 03:01
浏览 41
已采纳

角度和php中的ajax请求

I'm trying to start a simple web page in which I wanna load data to a combobox from a request to my php script; The problem that I'm having is that when I load the page it doesn't load, but if i assign the function to a button it does but i have to press it twice for it to start working... I just can't figure out why that's happening, I have the next code in my html:

<div class="form-group">
       <label class="col-md-6 col-xs-12" for="pref-perpage">Categoría:</label>
       <select id="pref-perpage" class="col-lg-9 lindo">
               <option ng-repeat="result in search.vendor">{{result.nombreCategoria}}</option>
               <option selected="selected">[ Seleccione ]</option>
       </select>
</div> <!-- form group [Categoria] -->

this is my javascript:

  search.buscar = function()
  {

    $.ajax({
      url: "Busqueda.php",
      type: "get",
      success: function(response) {
          search.vendor.push({'nombreCategoria':response});
        var JSONObject = JSON.parse(response);

          for(var x in parsed){
          search.vendor.push(parsed[x]);
        }

and the kind of data that I want to fill the array with would be something like:

search.vendor = [{'nombreCategoria':'data'}];

If've also tried the $http method from angular but it does the same; I fill the combobox with fake data manually and I have no problem!

server response: [{"nombreCategoria":"ACCESORIOS"},{"nombreCategoria":"CPU"},{"nombreCategoria":"IMPRESORA"},{"nombreCategoria":"MONITOR"}]

  • 写回答

1条回答 默认 最新

  • dtlrp119999 2016-04-17 18:36
    关注

    I'm working with Gabriel in this project. The soluction was to add the method $scope.$apply(); after the .push() method...

    So, the function looks like this:

    search.buscar = function() {
        $.ajax({
            url: "Busqueda.php",
            type: "get",
            success: function(response) {
                search.vendor.push({'nombreCategoria':response});
                var JSONObject = JSON.parse(response);
                for(var x in parsed) {
                    search.vendor.push(parsed[x]);
                    $scope.$apply();
                }
            }
        });
    }
    

    By the way... I found the solution here (stackoverflow)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!