Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在angularJs中将ajax响应推送到数组_Php_Angularjs_Ajax - Fatal编程技术网

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

Php 在angularJs中将ajax响应推送到数组,php,angularjs,ajax,Php,Angularjs,Ajax,我有一个数组对象的函数 $http({ method: 'GET', url: 'select.php' }).then(function success(response) { alert(response); //$scope.posts=response.data; }, function error(response) { // called asynchronously if

我有一个数组对象的函数

$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)           
    ];

}
在上面的代码中,我们传递了一些静态数据,但我尝试将来自另一个文件的一些动态数据作为ajax响应推送

因此,我如何才能使它充满活力。我试过这样做

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);
];
}
但它不起作用。你知道我该怎么做吗?

参考下面的代码:

$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); 
}

由于您希望以异步方式使用项目填充存储,因此需要重新考虑存储控制器以使用承诺:

function storeController($scope, $routeParams, DataService) {

  // get store from service
  ̶$̶s̶c̶o̶p̶e̶.̶s̶t̶o̶r̶e̶ ̶=̶ ̶D̶a̶t̶a̶S̶e̶r̶v̶i̶c̶e̶.̶s̶t̶o̶r̶e̶;̶

  var promise = DataService.getStore();

  promise.then(function(store) {
      $scope.store = store;
      return store;
  }).then(function(store) {
      // use routing to pick the selected product
      var sku = $routeParams.productSku
      if ( sku != null) {
        $scope.product = DataService.getProduct(store, sku);
      };
  });
}
然后DataService需要返回一个承诺:

storeApp.service("DataService", function($http) {
  ̶v̶a̶r̶ ̶m̶y̶S̶t̶o̶r̶e̶ ̶=̶ ̶n̶e̶w̶ ̶s̶t̶o̶r̶e̶(̶)̶;̶

  ̶t̶h̶i̶s̶.̶s̶t̶o̶r̶e̶ ̶=̶ ̶m̶y̶S̶t̶o̶r̶e̶;̶
  this.getStore = _getStore;
  this.getProduct = _getProduct;

  var dataPromise;
  function _getStore() { 
    if (! dataPromise) {
        dataPromise = getData();
    };   
    return dataPromise.then(function(data) {
       return data.map(item => new product(item));
    });
  }

  function getData() {
    var url = 'select.php';
    return $http.get(url)
      .then(function success(response) {
        return response.data; 
    }).catch(function error(response) {
        // called asynchronously if an error occurs
        console.log("ERROR: ",response.status);
        throw response;
    });
  }

  function _getProduct(products, sku) {
      for (var i = 0; i < products.length; i++) {
        if (products[i].sku == sku)
          return products[i];
      }
      return null;
  }
});

避免将诸如store之类的构造函数放在全局范围内。除了扰乱全局范围之外,他们还不能利用AngularJS服务,比如$http。而是将这些函数封装在AngularJS服务中,然后可以注入其他服务。

您必须放入$scope.product.pushresponse.data;在ajax请求$scope.products成功的情况下,而不是$scope.product成功的情况下,如果您没有注意到新产品的类型,那么应该根据数据的总行重复这一次,对吗?好的,那么我应该在这个.products=[new product]}函数存储{this.products=[//新产品APL,苹果,每…,12,90,0,2,0,1,2吃一个,$scope.products.pushresponse.data;}我已经编辑了我的代码,看看它。它会自动将你的新数据对象推送到数组中。这是我在控制台中遇到的错误$scope未定义,store未定义。你可以使用控制器注入进行发布。或者你可以用一些js工具进行更新。