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 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖