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)

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

报告相同问题?

悬赏问题

  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动