dpqaaczn141761 2017-07-25 07:19
浏览 88

在angularJs中将ajax响应推送到数组

I have a function with the array objects.

$http({
       method: 'GET',
       url: 'select.php'
     }).then(function success(response) {
          alert(response);
          //$scope.posts=response.data; 
     }, function error(response) {
          // called asynchronously if an error occurs
          // or server returns response with an error status.
     }
);
function store() {    
    this.products = [        
       new product("APL", "Apple", "Eat one every…", 12, 90, 0, 2, 0, 1, 2),
       new product("AVC", "Avocado", "Guacamole…", 16, 90, 0, 1, 1, 1, 2),
       new product("BAN", "Banana", "These are…", 4, 120, 0, 2, 1, 2, 2)           
    ];

}

In the above code we are passing some static data, but instead i am trying to a push some dynamic data which comes from another file as a ajax response.

so how can i make it dynamic. i have tried like this

function store() {
this.products = [    
    //new product("APL", "Apple", "Eat one every…", 12, 90, 0, 2, 0, 1, 2),
    //new product("AVC", "Avocado", "Guacamole…", 16, 90, 0, 1, 1, 1, 2),
    //new product("BAN", "Banana", "These are…", 4, 120, 0, 2, 1, 2, 2)
    $scope.product.push(response.data);
];
}

but its not working. any idea how can i make it?

  • 写回答

2条回答 默认 最新

  • douxuelv7755 2017-07-25 07:25
    关注

    Refer the below code:

    $scope.products = [];
        $http({
                      method: 'GET',
                      url: 'select.php'
                    }).then(function success(response) {
                        $scope.store(response.data);
                      }, function error(response) {
                        // called asynchronously if an error occurs
                        // or server returns response with an error status.
                      });
    
    $scope.store = function(data){
      $scope.products.push(data); 
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题